code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
//#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <array>
#include <unordered_map>
#include<unordered_set>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<complex>
#include<fstream>
#include<assert.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;
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; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int ans = 0;
vector<pii> P(N);
rep(i, 0, N)
{
cin >> P[i].first >> P[i].second;
}
rep(i, 0, N)rep(j, i + 1, N)
{
int dx = P[j].first - P[i].first;
int dy = P[j].second - P[i].second;
if (dx < 0)
{
dx *= -1;
dy *= -1;
}
if (-dx <= dy && dy <= dx)
{
ans++;
}
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long int
int fun(int a)
{
int ret=0;
while(a>0)
{
ret+=a%10;
a=a/10;
}
return ret
;}
main()
{
int n;
cin>>n;
float x[n],y[n];
for(int i=0;i<n;i++)
{
cin>>x[i]>>y[i];
}
int cou=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
float num=(y[i]-y[j])/(x[i]-x[j]);
if(num<=1&&num>=-1)
{
cou++;
}
}
}
cout<<cou<<endl;
} |
#include <iostream>
#include <cmath>
#include <string>
#include <array>
#include <queue>
using namespace std;
#define ll long long int
int max(int a, int b){
if (a<=b)
{
return b;
}
else{
return a;
}
}
void solve(){
ll n;
cin>>n;
ll a[n];
ll p[n];
ll maxval;
ll add;
ll prev;
ll prev1;
ll x[n];
prev=0;
add = 0;
maxval = 0;
prev1=0;
//out<<maxval<<add<<prev;
for (int i = 0; i < n; ++i)
{
cin>>a[i];
maxval = max(maxval, a[i]);
add+=a[i];
p[i] = add;
//prev = p[i];
x[i] = p[i]+prev1;
prev1=x[i];
//cout<<p[i];
cout<<x[i]+((i+1)*maxval)<<endl;
}
for (int i = 0; i < n; ++i)
{
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t;
t= 1;
for(int i = 0; i<t; i++){
solve();
}
return 0;
} | //
//#ifdef ONLINE_JUDGE
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("O2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//////
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#endif
#include <bits/stdc++.h>
#include <utility>
#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()
#define pb push_back
using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
typedef long double ld;
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
//using ll = long long;
//#ifdef __int128
//using hll = __int128;
//#endif
//using pii = pair<ll, ll>;
//using ld = long double;
template <typename T>
void do_uniq(vector<T> &vec){
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
}
#ifndef ONLINE_JUDGE
clock_t timestamp_start = clock();
void time_calc()
{
cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << endl;
}
#endif
#ifdef _getchar_nolock
#else
# define _getchar_nolock getchar_unlocked
#endif
#define integer ll
integer mod = 1e9 + 7;
integer ml(integer a, integer b) {
return (a * 1ll * b) % mod;
}
integer add(integer a, integer b) {
return (a + b) % mod;
}
integer sub(integer a, integer b) {
return add(a, mod - b);
}
integer sq(integer a) {
return ml(a, a);
}
integer b_p(integer b, integer p) {
if (p == 0) return 1;
if (p & 1) return ml(b, b_p(b, p - 1));
return sq(b_p(b, p >> 1));
}
int gcd(int x, int y){
return y == 0 ? x : gcd(y, x % y);
}
#define solvsh
//#define multi
#ifdef solvsh
//#define int ll
const int MAXN = 5e3 + 32;
void solve() {
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; ++i) {
int curr;
cin >> curr;
v.push_back(curr);
}
ll mmx = 0;
ll pref = 0;
ll add = 0;
for (int i = 0; i < n; ++i) {
mmx = max(mmx, 0ll + v[i]);
ll answr = (i + 1) * mmx;
pref += v[i];
add += pref;
answr += add;
cout << answr << "\n";
}
}
#else
#endif
void multisolve() {
#ifndef ONLINE_JUDGE
freopen("../input.in", "r", stdin);
#endif
int t;
cin >> t;
int i = 1;
while (t--) {
// cout << "Case #" << i << ": ";
solve();
// i++;
}
}
#ifndef ONLINE_JUDGE
void gen() {
#ifndef ONLINE_JUDGE
freopen("../input.in", "w", stdout);
#endif
}
#endif
#define int int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
#ifdef multi
// gen();
multisolve();
// checkk();
#else
solve();
#endif
// time_calc();
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
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;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
const int mod = 998244353;
struct mint {
ll x;
mint (ll x = 0) : x((x % mod + mod) % mod) {}
mint operator -() const {return mint(-x);}
mint& operator +=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator -=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator *=(const mint a) {(x *= a.x) %= mod; return *this;}
mint operator +(const mint a) const {return mint(*this) += a;}
mint operator -(const mint a) const {return mint(*this) -= a;}
mint operator *(const mint a) const {return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
mint inv() const {return pow(mod - 2);}
mint operator /=(const mint a) {return *this *= a.inv();}
mint operator /(const mint a) {return mint(*this) /= a;}
};
istream& operator >>(istream &is, mint &a) {return is >> a.x;}
ostream& operator <<(ostream &os, const mint &a) {return os << a.x;}
mint solve(int n, int m, int k) {
if (n == 1 || m == 1) return mint(k).pow(m + n - 1);
mint ans = 0;
for (int x = 1; x <= k; x++) {
ans += (mint(x).pow(n) - mint(x - 1).pow(n)) * mint(k - x + 1).pow(m);
}
return ans;
}
int main() {
int n, m, k; cin >> n >> m >> k;
cout << solve(n, m, k) << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pair<int, int> >
#define REP(i,a,b) for (int i = a; i <= b; i++)
const int mod = 1e9 + 7;
const int INF = INT_MAX;
const int NINF = INT_MIN;
int a, b, c;
void Solve() {
cin>>a>>b>>c;
a = 7 - a;
b = 7 - b;
c = 7 - c;
cout<<(a + b+ c)<<"\n";
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
} |
#include <bits/stdc++.h>
#define lowbit(x) ((x) & (-x))
using namespace std;
typedef long long LL;
const int N = 200005;
int n, e;
int a[N], b[N], c[N], d[N * 4];
LL m, f[N * 4];
void init() {
memset(f, 0, sizeof(f));
}
void add(int p, int val) {
for (; p < e; p += lowbit(p)) f[p] += val;
}
LL cal(int p) {
LL ans = 0;
for (; p > 0; p -= lowbit(p)) ans += f[p];
return ans;
}
void gao() {
e = 0;
d[e++] = 0;
for (int i = 0; i < n; i++) {
d[e++] = a[i], d[e++] = a[i] + 1;
d[e++] = b[i], d[e++] = b[i] + 1;
}
sort(d, d + e);
e = unique(d, d + e) - d;
for (int i = 0; i < n; i++) {
a[i] = lower_bound(d, d + e, a[i]) - d;
b[i] = lower_bound(d, d + e, b[i]) - d;
// printf("l= %d, r= %d, c= %d\n", a[i], b[i], c[i]);
}
init();
for (int i = 0; i < n; i++) {
add(a[i], c[i]);
add(b[i] + 1, -c[i]);
}
LL ans = 0;
for (int i = 1; i < e - 1; i++) {
LL tmp = cal(i);
// printf("i= %d %d %lld %lld\n", i, d[i], tmp, tmp * (d[i + 1] - d[i]));
ans += min(m, tmp) * (d[i + 1] - d[i]);
}
cout << ans << '\n';
}
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
int main() {
scan_d(n);
scanf("%lld", &m);
for (int i = 0; i < n; i++) scan_d(a[i]), scan_d(b[i]), scan_d(c[i]);
gao();
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long int
#define mod 1000000007
#define MAXN 200001
typedef tree<int, null_type,
less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
Ordered_set;
// s.find_by_order(idx) find using index..!
// s.order_of_key(val) Gives iterator to the key..!
/* First solve then try Writing Code.. And be cool ^_^ and calm. */
int n, a[MAXN];
int go(int l, int r)
{
if(r < l)
return 0;
if(r == l)
return a[r];
int idx = l;
for(int i=l;i<=r;i++)
{
if(a[i] < a[idx])
idx = i;
}
int fi = go(l, idx-1);
int se = go(idx+1, r);
if(a[idx]*(r-l+1) > max(fi, se))
return a[idx]*(r-l+1);
else
return max(fi, se);
}
void solve(){
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<go(0, n-1)<<endl;
}
int32_t main()
{
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}
|
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
int n;cin>>n;
int a[n],b[n],c[n],d[n],e[n];
for(int i=0;i<n;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i]>>e[i];
}
int minn=0;int maxx=1e9;int ans=0;
while(minn<=maxx){
int midd=minn+(maxx-minn)/2;
/*int cnt[32];
for(int i=0;i<32;i++){
cnt[i]=0;
}*/
set<int>cnt;
for(int i=0;i<n;i++){
int val=0;
if(a[i]>=midd)val=(val|(1<<4));
if(b[i]>=midd)val=(val|(1<<3));
if(c[i]>=midd)val=(val|(1<<2));
if(d[i]>=midd)val=(val|(1<<1));
if(e[i]>=midd)val=(val|(1<<0));
cnt.insert(val);
}
int f=0;
/*for(int i=0;i<32;i++){
if(cnt[i]==0)continue;
if(i==31 && cnt[i]>=3){
f=1;break;
}
for(int j=i+1;j<32;j++){
if(cnt[j]==0)continue;
int val=(i|j);
if(cnt[i]+cnt[j]>=3 && val==31){
f=1;break;
}
for(int k=j+1;k<32;k++){
if(cnt[k]==0)continue;
val=(val|k);
if(val==31){
f=1;break;
}
}
if(f==1)break;
}
if(f==1)break;
}*/
for(int x:cnt){
for(int y:cnt){
for(int z:cnt){
if((x|y|z)==31){
f=1;break;
}
}
if(f==1)break;
}
if(f==1)break;
}
if(f==1){
ans=max(ans,midd);
minn=midd+1;
}
else maxx=midd-1;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
map<ll, bool> mp; //防止重复计算
int main()
{
ll n, s = 0;
cin >> n;
ll k = sqrt(n);
for (ll a = 2; a <= k; a++) //底数从2枚举到√n
{
ll b = 2; //指数从2开始枚举
while (1)
{
ll c = pow(a, b);
if (c > n) //当a^b大于n时,结束循环
break;
if (!mp[c]) //如果还没有出现过
{
mp[c] = 1; //记录下这个可展开数
s++;
}
b++;
}
}
printf("%lld\n", n - s);
return 0;
} |
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
int main()
{
int n;
cin >> n;
vector<pair<ll, int>> xs, ys;
rep(i, n)
{
ll x, y;
cin >> x >> y;
xs.push_back({x, i});
ys.push_back({y, i});
}
sort(ALL(xs));
sort(ALL(ys));
vector<pair<ll, ll>> ans;
ans.push_back({xs[xs.size() - 1].first - xs[0].first, xs[xs.size() - 1].second * 100000ll + xs[0].second});
ans.push_back({xs[xs.size() - 2].first - xs[0].first, xs[xs.size() - 2].second * 100000ll + xs[0].second});
ans.push_back({xs[xs.size() - 1].first - xs[1].first, xs[xs.size() - 1].second * 100000ll + xs[1].second});
ans.push_back({ys[ys.size() - 1].first - ys[0].first, ys[ys.size() - 1].second * 100000ll + ys[0].second});
ans.push_back({ys[ys.size() - 2].first - ys[0].first, ys[ys.size() - 2].second * 100000ll + ys[0].second});
ans.push_back({ys[ys.size() - 1].first - ys[1].first, ys[ys.size() - 1].second * 100000ll + ys[1].second});
sort(ALL(ans), greater<pair<ll, ll>>());
ll id = ans[0].second;
rep(i, ans.size())
{
if (ans[i].second != id)
{
cout << ans[i].first << endl;
return 0;
}
}
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pii pair<int, int>
const int N = 1e5 + 5;
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pii> vx, vy, v;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back({x, y});
vx.push_back({x, i});
vy.push_back({y, i});
}
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
set<int> s;
s.insert(vx[0].second);
s.insert(vx[1].second);
s.insert(vx[n - 1].second);
s.insert(vx[n - 2].second);
s.insert(vy[0].second);
s.insert(vy[1].second);
s.insert(vy[n - 1].second);
s.insert(vy[n - 2].second);
vector<int> q;
for (auto it: s) {
q.push_back(it);
}
vector<int> dist;
for (int i = 0; i < q.size(); i++)
for (int j = i + 1; j < q.size(); j++) {
dist.push_back(max(
abs(v[q[i]].first - v[q[j]].first),
abs(v[q[i]].second - v[q[j]].second)
));
}
sort(dist.begin(), dist.end());
dist.pop_back();
cout << dist.back();
} |
#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on
struct in {
template <class T> operator T() {
T t;
std::cin >> t;
return t;
}
};
void out() { std::cout << "\n"; }
template <class Head, class... Tail> void out(Head &&h, Tail &&... t) {
std::cout << h << (sizeof...(Tail) == 0 ? "" : " "), out(std::forward<Tail>(t)...);
}
/**
* author: knshnb
* created: Sat Oct 24 21:04:11 JST 2020
**/
struct UnionFind {
int cnt; // 集合の数
std::vector<int> number; // 0以上のとき親のindex, 負のときは集合サイズ
std::vector<Int> a, b;
UnionFind(int n) : cnt(n), a(n), b(n), number(n, -1) {}
int root(int x) { return number[x] < 0 ? x : number[x] = root(number[x]); }
void unite(int x, int y) {
x = root(x), y = root(y);
if (x == y) return;
if (number[y] < number[x]) std::swap(x, y);
// yをxにマージ
a[x] += a[y];
b[x] += b[y];
number[x] += number[y];
number[y] = x;
cnt--;
}
bool is_same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -number[root(x)]; }
};
signed main() {
Int n = in(), m = in();
UnionFind uf(n);
REP(i, n) std::cin >> uf.a[i];
REP(i, n) std::cin >> uf.b[i];
REP(i, m) {
Int u = (Int)in() - 1, v = (Int)in() - 1;
uf.unite(u, v);
}
REP(i, n) {
Int r = uf.root(i);
if (uf.a[r] != uf.b[r]) {
out("No");
return 0;
}
}
out("Yes");
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007;
#ifndef ONLINE_JUDGE
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;}
ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;}
template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print_sim_py(void) {cout << endl;}
template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();}
template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);}
#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...);
#endif
template <typename... Ts>
std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); }
#define repr(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll a,b,w;
IN(a,b,w);
w*=1000;
ll amin, amax;
amin = 1000001;
amax = -1;
repr(i, 1, 1000001){
if(i*a <= w && w <= i*b){
amin = min(amin, i);
amax = max(amax, i);
}
}
if(amax == -1){
cout << "UNSATISFIABLE" << endl;
return 0;
}
cout << amin << " " << amax;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using Graph = vector<vector<int>>;
struct Edge {
int to;
int cost;
Edge(int t, int c) : to(t), cost(c) {}
};
using edge = struct {
long long to;
long long cost;
};
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 cnt[101010];
bool good[101010];
void dfs(const Graph& g, int c, int parent, const vector<int>& C) {
if (cnt[C[c]] == 0) good[c] = true;
cnt[C[c]]++;
for (auto next_v : g[c]) {
if (next_v == parent) continue;
dfs(g, next_v, c, C);
}
cnt[C[c]]--;
}
int main() {
int N;
cin >> N;
vector<int> C(N + 1);
rep(i, N) cin >> C[i + 1];
Graph g(N + 1);
rep(i, N - 1) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(g, 1, -1, C);
rep(i, N) if (good[i + 1]) cout << i + 1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(int i, int p, vector<int> &colors, vector<vector<int>> &adj_list, set<int> &used_color, vector<bool> &ans) {
bool add_color;
if (used_color.count(colors[i])) {
add_color = false;
ans[i] = false;
} else {
add_color = true;
used_color.insert(colors[i]);
}
if (i == 0) {
for (int c : adj_list[0]) {
dfs(c, 0, colors, adj_list, used_color, ans);
}
} else {
if (adj_list[i].size() == 1) {
if (add_color) {
used_color.erase(colors[i]);
}
return;
} else {
for (int c : adj_list[i]) {
if (c != p) {
dfs(c, i, colors, adj_list, used_color, ans);
}
}
if (add_color) {
used_color.erase(colors[i]);
}
}
}
}
int main() {
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
vector<vector<int>> adj_list(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
adj_list[a].emplace_back(b);
adj_list[b].emplace_back(a);
}
set<int> used_color;
vector<bool> ans(n, true);
dfs(0, -1, c, adj_list, used_color, ans);
for (int i = 0; i < n; ++i) {
if (ans[i]) {
cout << i + 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);
int qq;cin>>qq;
while(qq--){
ll n;cin>>n;
ll tcnt=0;
while(n%2==0){
tcnt++;
n/=2;
}
if(tcnt==1){
cout<<"Same"<<ENDL;
}else if(tcnt>1)cout<<"Even"<<ENDL;
else cout<<"Odd"<<ENDL;
}
}
//(・_・)(・_・)(・_・)(・_・)
//CHECK overflow,what to output?
//any other simpler approach? | #include<bits/stdc++.h>
#define ll long long
typedef unsigned long long ull;
using namespace std;
const int Max=1e6+20;
const int Maxn=1e7+20;
const int mod=1e9+7;
const int c=231;
inline int read()
{
int f=1,x=0;char ch;
do{ch=getchar();if(ch=='-')f=-1;}
while(ch<'0'||ch>'9');
do{x=x*10+ch-'0';ch=getchar();}
while(ch>='0'&&ch<='9');return x*f;
}
int main()
{
int t=read();
ll n;
while(t--)
{
cin>>n;
if(n%4==0) cout<<"Even"<<endl;
else if(n%2==0) cout<<"Same"<<endl;
else cout<<"Odd"<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 2000000000;
const int MOD = 1e9 + 7;
int main(){
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
int ans = 0;
rep(i,n){
if(a[i] <= 10) continue;
a[i] -= 10;
ans += a[i];
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define fast_IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define rep(i,a,b) for (int i = a; i < b; i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define pii pair<int,int>
#define ar(n) array<int,n>
#define sz(n) (int)n.size()
#define uniq(v) (v).erase(unique(v.begin(), v.end()),(v).end())
#ifndef ONLINE_JUDGE
#define dbg(x) cerr<<#x<<" "<<x<<endl;
#else
#define dbg(x)
#endif
const int inf=1e18;
const int32_t M=1e9+7;
const int32_t mxn=1e6+1;
template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a) {for (auto& i : a) out << i << " "; return out;}
auto clk = clock();
mt19937_64 rang(high_resolution_clock::now().time_since_epoch().count());
void run_time()
{
cerr << endl;
cerr << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
return;
}
/*-------------------------------------solve-----------------------------------------*/
void solve()
{
int n; cin>>n;
vector<int> v(n); cin>>v;
int ans=0;
rep(i,0,n) if(v[i]>10) ans+=abs(10-v[i]);
cout<<ans<<endl;
}
int32_t main()
{
fast_IO
#ifndef ONLINE_JUDGE
freopen("error.txt","w",stderr);
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
run_time();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long double a,b;
int main(){
cin>>a>>b;
cout<<fixed<<setprecision(20)<<100*(a-b)/a;
}/*
00100001000010000100
*/ | #include<iostream>
using namespace std;
int main()
{
float A, B, C;
float D;
cin >> A >> B;
C = A-B;
D = (C/A)*100.0;
if(A > B)
cout << D;
else
return -1;
return 0;
}
|
#include <bits/stdc++.h>
#define index(i, n) for (long long i = 0; i < n; i++)
#define index1(i, n) for (long long i = 1; i <= n; i++)
#define range(i, a, b) for (long long i = a; i < b; i++)
#define indexD(i, n) for (long long i = n - 1; i >= 0; i--)
#define index1D(i, n) for (long long i = n; i >= 1; i--)
#define rangeD(i, a, b) for (long long i = a - 1; i >= b; i--)
#define ceild(a, b) ((a) / (b) + ((a) % (b) != 0))
#define fast_io std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define print(x) cout << (x) << '\n'
#define trace(x) cout << '>' << #x << ':' << x << endl;
#define trace2(x, y) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << endl;
#define trace3(x, y, z) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << " | " << #z << ':' << z << endl;
#define trace4(x, y, z, w) cout << '>' << #x << ':' << x << " | " << #y << ':' << y << " | " << #z << ':' << z << " | " << #w << ':' << w << endl;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define First first
#define Second second
#define MOD 1000000007
#define INF (9223372034707292159ll)
typedef unsigned long long ull;
typedef long long int ll;
using namespace std;
int main()
{
fast_io;
ll t, n, m, k, x, y, r, ans, c;
ull pow2[21];
pow2[0] = 1;
index1(i, 20) { pow2[i] = 2 * pow2[i - 1]; }
ans = 0;
cin >> n >> m;
string s;
ll even = 0, odd = 0;
index(i, n)
{
cin >> s;
x = 0;
index(j, m) x += (s[j] == '1');
if (x % 2)
odd++;
else
even++;
}
print((odd * even));
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
int a[2];
int main() {
int n, m;
string s;
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
cin >> s;
int x = 0;
for(int j = 0; j < m; ++j) {
x ^= (s[j] - '0');
}
a[x]++;
}
print(1ll * a[1] * a[0]);
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
bool A[110][110] = {};
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
char c; cin >> c; A[i][j] = c - '0';
}
}
double ans = 0;
for(int i = 0; i < N; i++){
bool ok[110] = {};
ok[i] = true;
queue<int> que;
que.push(i);
while(que.size()){
int p = que.front();
que.pop();
for(int i = 0; i < N; i++){
if(A[i][p] && !ok[i]){
ok[i] = true; que.push(i);
}
}
}
int cnt = 0;
for(int i = 0; i < N; i++){
if(ok[i]) cnt ++;
}
ans += (double) 1.0 / cnt;
}
cout.precision(15);
cout << ans << "\n";
} | #pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;
typedef pair <int, int> PII;
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); }
const int maxN = 123;
int n, vis[maxN], cnt[maxN], visCtr;
char G[maxN][maxN];
void dfs(int v)
{
vis[v] = visCtr;
cnt[v]++;
FOR(u, 1, n+1)
if (G[v][u] == '1' and vis[u] != visCtr)
dfs(u);
}
void solve()
{
scanf ("%d", &n);
FOR(i, 1, n+1)
scanf ("%s", &G[i][1]);
FOR(i, 1, n+1)
{
visCtr = i;
dfs(i);
}
long double res = 0;
FOR(i, 1, n+1)
res += 1.0 / cnt[i];
printf("%.13Lf\n", res);
}
int main()
{
int t = 1;
//scanf ("%d", &t);
FOR(tid, 1, t+1)
{
//printf("Case #%d: ", tid);
solve();
}
return 0;
}
|
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
int n, m;
int a[200002];
signed main() {
int i;
cin >> n >> m;
rep(i, m) cin >> a[i];
a[m] = 0; a[m + 1] = n + 1;
m += 2;
sort(a, a + m);
int k = n;
rep(i, m - 1) {
if (a[i + 1] - a[i] >= 2) {
k = min(k, a[i + 1] - a[i] - 1);
}
}
int ans = 0;
rep(i, m - 1) {
if (a[i + 1] - a[i] >= 2) {
ans += ((a[i + 1] - a[i] - 1) + k - 1) / k;
}
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
set<int> getFactors(ll n) {
set<int> st;
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
st.insert(i);
st.insert(n / i);
}
}
return st;
}
void solve() {
int N, M; cin >> N >> M;
vector<int> A (M + 2);
A[0] = 0;
A[M + 1] = N + 1;
for (int i = 1; i < M + 1; ++i) cin >> A[i];
sort( A.begin(), A.end() );
set<int> factors;
for (int i = 1; i < M + 2; ++i) {
int diff = A[i] - A[i - 1] - 1;
if (diff > 0)
factors.emplace(diff);
}
int ans = INT_MAX;
int f = *factors.begin();
int result = 0;
for (int i = 1; i < M + 2; ++i) {
int diff = A[i] - A[i - 1] - 1;
if (diff <= 0) continue;
if (diff % f != 0) { result++; }
result += diff / f;
}
ans = min(ans, result);
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
while(t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed
#define endl '\n'
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
void solve(istream& cin, ostream& cout) {
int t;
cin >> t;
REP(i, t) {
ll l, r, ans = 0;
cin >> l >> r;
if (r - l * 2 >= 0) {
ans += (r - l * 2 + 2) * (r - l * 2 + 1) / 2;
}
cout << ans << endl;
}
}
#ifndef TEST
int main() {
OPTIMIZE_STDIO;
solve(cin, cout);
return 0;
}
#endif
| #include<bits/stdc++.h>
using namespace std;
#define int long long
inline void solve()
{
int L,R;
cin>>L>>R;
if (2*L>R) cout<<0<<endl;
else cout<<(R-2ll*L+1ll)*(R-2ll*L+2ll)/2ll<<endl;
}
signed main()
{
ios::sync_with_stdio(false); cout.tie(NULL);
int t;
cin>>t;
for (int i=1;i<=t;i++) solve();
} |
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vi2 = vector<vector<ll>>;
using vl = vector<ll>;
using vl2 = vector<vector<ll>>;
using vb = vector<bool>;
using vb2 = vector<vector<bool>>;
using vc = vector<char>;
using vc2 = vector<vector<char>>;
using vs = vector<string>;
using si = set<int>;
using vs2 = vector<vector<string>>;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9+1;
const ll LINF = 1e18+1;
const long double LPI = acos(-1.0);
const double PI = acos(-1.0);
const int mod = 1000000007;
#define MP(a,b) make_pair((a),(b))
#define MT(...) make_tuple(__VA_ARGS__)
#define sz(x) (int)(x).size()
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,x) for(ll i=0;i<(int)(x);i++)
#define REPS(i,x) for(ll i=1;i<=(int)(x);i++)
#define RREP(i,x) for(ll i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(ll i=((int)(x));i>0;i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SORT(v) sort(ALL(v));
#define RSORT(v) sort(RALL(v));
#define IN(type,a) type a;cin >> a;
#define VIN(type,a,n) vector<type> a(n);cin >> a;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl;
#define COUT(x) cout << (x) << endl;
#define DCOUT(x,n) cout << fixed << setprecision(n) << (x) << endl;
#define ENDL cout << endl;
#define DB(s,x) cout << (s) << " : " << x << endl;
#define HOGE COUT("HOGE")
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,sz(v))i>>v[j];return i;}
template<typename T> bool chmax(T& a, const T& b){if(a<b){a=b;return true;}return false;}
template<typename T> bool chmin(T& a, const T& b){if(a>b){a=b;return true;}return false;}
template<typename T> T gcd(T a,T b){if(b==0){return a;}else{return gcd(b, a%b);}}
template<typename T> T lcm(T a,T b){return a*b/gcd(a, b);}
template<typename T> bool isPrime(T a){if(a<=1){return false;}
for(T i=2;i*i<=a;i++){if (a%i==0) {return false;}}return true;}
template<typename T> T ord(T p,T n){T ans=0;while(true){if(n%p==0){n/=p;ans++;}else{break;}}return ans;}
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main() {
ll x,y,a,b;
cin >> x >> y >> a >> b;
ll ans = 0;
while (x<=b/(a-1)&&x*a<y) {
ans++;
x *= a;
}
ans += (y-x-1)/b;
COUT(ans);
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define MOD 1000000007
int main() {
ll x, y;
cin >> x >> y;
ll a, b;
cin >> a >> b;
ll ans = 0;
for (int t = 0; t <= 60; ++t) {
bool ng = false;
ll str = x, exp = 0;
ll cnt = t;
while ((double)str * a < (double)y && cnt) {
str *= a;
exp++;
cnt--;
}
if (cnt != 0) continue;
ll u = (y - str) / b;
str += b * u;
exp += u;
if (y <= str) {
exp--;
}
ans = max(ans, exp);
}
cout << ans << endl;
return 0;
} |
#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(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
struct Ad{ll x, y, r, num;};
struct Recang{
ll x1, y1, x2, y2;
ll size(){
return abs(x2-x1)*abs(y2-y1);
}
};
const int MAX=9999;
int n;
Ad ads[300];
Recang ans[300];
bool overlap(Recang a, Recang b){
return max(a.x1,b.x1)<min(a.x2,b.x2)&&max(a.y1,b.y1)<min(a.y2,b.y2);
}
bool check(Recang a, int k){
REP(i,n){
if(i==k)
continue;
if(overlap(a,ans[i]))
return 1;
}
return 0;
}
void solve(){
cin >> n;
int x, y, r;
REP(i,n){
cin >> x >> y >> r;
ads[i]=Ad{x,y,r,i};
}
sort(ads,ads+n,[](const Ad& a, const Ad& b){return a.r<b.r;});
REP(i,n){
Ad ad=ads[i];
ans[ad.num]={ad.x,ad.y,ad.x+1,ad.y+1};
}
REP(i,n){
Ad ad=ads[i];
Recang tmpr={ad.x,ad.y,ad.x+1,ad.y+1};
while(1){
bool ok=0;
tmpr.x1--;
if(tmpr.x1<0||check(tmpr,ad.num)) tmpr.x1++;
else ok=1;
if(tmpr.size()>ad.r) break;
tmpr.y1--;
if(tmpr.y1<0||check(tmpr,ad.num)) tmpr.y1++;
else ok=1;
if(tmpr.size()>ad.r) break;
tmpr.x2++;
if(tmpr.x2>MAX||check(tmpr,ad.num)) tmpr.x2--;
else ok=1;
if(tmpr.size()>ad.r) break;
tmpr.y2++;
if(tmpr.y2>MAX||check(tmpr,ad.num)) tmpr.y2--;
else ok=1;
if(tmpr.size()>ad.r) break;
if(!ok) break;
}
ans[ad.num]=tmpr;
}
REP(i,n){
Recang tmpr=ans[i];
cout << tmpr.x1 << " ";
cout << tmpr.y1 << " ";
cout << tmpr.x2 << " ";
cout << tmpr.y2 << endl;
}
}
int main(){
solve();
return 0;
} | /*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
return f.second > s.second;
}
ll gcd(ll a, ll b){
if (b == 0)return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b){
return a / gcd(a, b) * b;
}
ll conbinationMemo[201][12];
void cmemoInit(){
rep(i, 201){
rep(j, 12){
conbinationMemo[i][j] = -1;
}
}
}
ll nCr(ll n, ll r){
if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
if(r == 0 || r == n){
return 1;
} else if(r == 1){
return n;
}
return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r){
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--) ret *= i;
return ret;
}
//-----------------------ここから-----------
int main(void){
ll n, m;
cin >> n >> m;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<vector<ll>> pos(2 * 1e6);
rep(i, 2 * 1e6) pos[i].push_back(-1);
rep(i, n){
pos[a[i]].push_back(i);
}
rep(i, 2 * 1e6) pos[i].push_back(n);
ll ans = n;
rep(i, n){
rep(j, pos[i].size() - 1){
if(abs(pos[i][j] - pos[i][j + 1]) > m){
ans = min(ans, (ll)i);
}
}
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
#define ll long long
#define min(x,y) x<y?x:y
#define max(x,y) x>y?x:y
using namespace std;
int main()
{
ll n,k;
cin >> n>>k;
while (k--)
{
if(n%200==0)
{
n=n/200;
}
else
{
n = (n*1000)+200;
}
}
cout << n <<endl;
} | /**
* author: Taichicchi
* created: 13.04.2021 21:52:36
**/
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
int M, H;
cin >> M >> H;
cout << ((H % M == 0) ? "Yes" : "No") << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int mod = 200;
int n; cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++){
int ai; cin >> ai;
ai %= mod;
a[i] = ai;
}
vector<vector<uint>> b(200);
n = min(8,n);
for(uint i = 1; i < (1<<n); i++){
int ai = 0;
for(int j = 0; j < n; j++){
if(i>>j&1) ai = (ai+a[j])%200;
}
b[ai].push_back(i);
}
bool bl = false;
int ansi;
for(int i = 0; i < 200; i++){
if(b[i].size() > 1){
bl = true;
ansi = i;
break;
}
}
if(bl){
cout << "Yes" << endl;
vector<vector<int>> ans(2);
for(int i = 0; i < 2; i++){
uint j = b[ansi][i];
for(int k = 0; k < n; k++){
if(j>>k&1) ans[i].push_back(k+1);
}
}
for(vector<int> an : ans){
cout << an.size();
for(int ani : an) cout << " " << ani;
cout << endl;
}
}
else cout << "No" << endl;
/*for(auto bi : b){
for(auto bij : bi){
cout << bij << ",";
}
cout << endl;
}*/
/*auto start = chrono::system_clock::now();
auto end = chrono::system_clock::now();
auto dur = end-start;
auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
cout << msec << "[ms]" << endl;*/
} | #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<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ff first
#define ss second
#define pi pair<int, int>
const int MX=300000;
const ll mod=1e9+7;
int dp[100];
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin>>n;
set<int>st;
set<int>::iterator it;
for(int i=1; i<=min(n-2, 1666); i++) st.insert(6*i);
for(int i=1; ; i++)
{
if(st.size()==n-1 || 10*i>10000) break;
st.insert(10*i);
}
for(int i=1; ; i++)
{
if(st.size()==n) break;
st.insert(15*i);
}
for(it=st.begin(); it!=st.end(); it++) cout<<*(it)<<' ';
cout<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
#define rep(i,n) for(int i=0;i<n;i++)
using P=pair<int,int>;
int n;
string s,t;
bool bo(){
int s1=0,t1=0;
rep(i,n){
if(s[i]=='1') s1++;
if(t[i]=='1') t1++;
}
return (s1==t1);
}
int main(){
cin >> n >> s >> t;
vector<int> s1(0);
vector<int> t1(0);
rep(i,n){
if(s[i]=='0') s1.push_back(i);
if(t[i]=='0') t1.push_back(i);
}
int sn=s1.size();
int tn=t1.size();
if(sn!=tn) cout << -1 << endl;
else{
int ans=0;
rep(i,sn){
if(s1[i]!=t1[i]) ans++;
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
int K; string S, T; cin >> K >> S >> T;
vector<ll> cards(10, K), scards(10, 0), tcards(10, 0);
for (auto c : S) {
if (c == '#') break;
cards[c - '0']--;
scards[c - '0']++;
}
for (auto c : T) {
if (c == '#') break;
cards[c - '0']--;
tcards[c - '0']++;
}
vector<ll> multiples(10, 1);
REP(i, 1, 10) multiples[i] = multiples[i - 1] * 10;
ll s = 0, t = 0;
REP(i, 1, 10) {
s += multiples[scards[i]] * i;
t += multiples[tcards[i]] * i;
}
// cout << "### base : " << s << ", " << t << endl;
ll win = 0, lose = 0;
REP(i, 1, 10) {
if (!cards[i]) continue;
cards[i]--;
s -= multiples[scards[i]] * i;
s += multiples[scards[i] + 1] * i;
// cout << "# i : " << i << ", s : " << s << endl;
REP(j, 1, 10) {
if (!cards[j]) continue;
t -= multiples[tcards[j]] * j;
t += multiples[tcards[j] + 1] * j;
// cout << "# j : " << j << ", t : " << t << endl;
if (s > t) {
win += (cards[i] + 1) * cards[j];
} else {
lose += (cards[i] + 1) * cards[j];
}
t -= multiples[tcards[j] + 1] * j;
t += multiples[tcards[j]] * j;
}
s -= multiples[scards[i] + 1] * i;
s += multiples[scards[i]] * i;
cards[i]++;
}
// cout << win << ", " << lose << endl;
cout << fixed << setprecision(15);
cout << (long double)(win) / (win + lose) << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, N) for(int i = 0; i< N; i++)
typedef long long ll;
int main() {
int n, m; cin >> n >> m;
vector<int> h(n), w(m);
rep(i, n) cin >> h[i];
rep(i, m) cin >> w[i];
sort(h.begin(), h.end());
sort(w.begin(), w.end());
//左詰での身長差の累積和と右詰での累積和
vector<int> left, right;
int lsum = 0, rsum = 0;
int ans = 1001001001;
if(h.size() == 1) {
auto it = lower_bound(w.begin(), w.end(), h[0]);
int idx = it - w.begin();
if(it == w.end()) {
idx = w.size()-1;
}
if(idx == 0) {
ans = abs(h[0] - w[idx]);
} else {
ans = min(abs(h[0] - w[idx]), abs(h[0] - w[idx-1]));
}
cout << ans << endl;
return 0;
}
for(int i = 0; i < n-1; i += 2) {
lsum += h[i+1] - h[i];
rsum += h[i+2] - h[i+1];
left.push_back(lsum);
right.push_back(rsum);
}
for(int i = 0; i < n; i += 2) {
int tmp;
if(i == 0) {
tmp = right.back();
}
else tmp = left[i/2-1] + right.back() - right[i/2-1];
//先生の分を足す
auto it = lower_bound(w.begin(), w.end(), h[i]);
int idx = it - w.begin();
if(it == w.end()) {
idx = w.size()-1;
}
if(idx == 0) {
tmp += abs(h[i] - w[idx]);
} else {
tmp += min(abs(h[i] - w[idx]), abs(h[i] - w[idx-1]));
}
ans = min(ans, tmp);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
typedef long long LL;
#define lowbit(x) (x & (-x))
const LL inf = 1e17+9;
const LL mod = 1e9+7;
const LL maxn = 2e5+8;
LL n, m, h[maxn], w[maxn], sa[maxn], sb[maxn];
void solve() {
LL i, j, ans = inf;
scanf("%lld%lld", &n, &m);
for (i = 1; i <= n; i++) scanf("%lld", &h[i]);
for (i = 1; i <= m; i++) scanf("%lld", &w[i]);
std::sort(h + 1, h + 1 + n);
for (i = 2; i <= n; i += 2) sa[i] = sa[i - 2] + h[i] - h[i - 1];
for (i = n - 1; i >= 1; i -= 2) sb[i] = sb [i + 2] + h[i + 1] - h[i];
for (i = 1; i <= m; i++) {
LL pos = std::lower_bound(h + 1, h + 1 + n, w[i]) - h;
if (pos == 0) ans = std::min(ans, sb[2] + abs(h[1] - w[i]));
else if (pos & 1) ans = std::min(ans, sa[pos - 1] + sb[pos + 1] + abs(h[pos] - w[i]));
else ans = std::min(ans, sa[pos - 2] + sb[pos] + abs(h[pos - 1] - w[i]));
}
printf("%lld\n", ans);
}
signed main() {
LL t = 1; //scanf("%lld", &t);
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a; i >-1; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a; i > b; --i)
#define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define fi first
#define se second
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll mod_998244353 = 998244353;
constexpr ll INF = 1LL << 60;
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
//using lll=boost::multiprecision::cpp_int;
//using Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<128>>;//仮数部が1024桁
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;
}
ll mypow(ll x, ll n, const ll &p = -1)
{ //x^nをmodで割った余り
if (p != -1)
{
x =(x%p+p)%p;
}
ll ret = 1;
while (n > 0)
{
if (n & 1)
{
if (p != -1)
ret = (ret * x) % p;
else
ret *= x;
}
if (p != -1)
x = (x * x) % p;
else
x *= x;
n >>= 1;
}
return ret;
}
using namespace std;
//using namespace atcoder;
template<typename T>
struct BIT{
//0オリジンで与える
vector<T>bit;
int N;
BIT(int n):N(n){
bit.assign(++n,0);
}
T sum(int i){
T s=0;
++i;//0オリジンに対応させる
while(i>0){
s+=bit[i];
i-=i&-i;//iから最後の1のビットを減算
}
return s;
}
void add(int i,T x){
++i;//0オリジンに対応させる
while(i<(int)bit.size()){
bit[i]+=x;
i+=i&-i;
}
}
T Lower_bound(T k){
if(k<=0)
return 0;
T x = 0;
T mul = 1;
while (mul<N){
mul *= 2;
}
for (T i = mul; i > 0;i/=2){
if(x+i<=N&&bit[x+i]<k){
k -= bit[x + i];
x += i;
}
}
return x;
}
};
using P=pair<ll,int>;
void solve()
{
int n,m,q;
cin>>n>>m>>q;
vector<int>t(q),x(q);
vector<ll>y(q);
rep(i,q)cin>>t[i]>>x[i]>>y[i],x[i]--;
vector<ll>prea(n),preb(m);
vector<int>id(q);
rep(i,q)id[i]=y[i];
id.eb(0);
sort(all(id));
BIT<ll>a(q+1),b(q+1);
BIT<int>aid(q+1),bid(q+1);
aid.add(0,n);
bid.add(0,m);
ll ans=0;
rep(i,q){
if(t[i]==1){
int pos=lower_bound(all(id),prea[x[i]])-id.begin();
ll val=bid.sum(pos)*prea[x[i]];
val+=b.sum(q+1)-b.sum(pos);
ans-=val;
aid.add(pos,-1);
a.add(pos,-prea[x[i]]);
prea[x[i]]=y[i];
pos=lower_bound(all(id),prea[x[i]])-id.begin();
val=bid.sum(pos)*prea[x[i]];
val+=b.sum(q+1)-b.sum(pos);
ans+=val;
aid.add(pos,1);
a.add(pos,prea[x[i]]);
}else{
int pos=lower_bound(all(id),preb[x[i]])-id.begin();
ll val=aid.sum(pos)*preb[x[i]];
val+=a.sum(q+1)-a.sum(pos);
ans-=val;
bid.add(pos,-1);
b.add(pos,-preb[x[i]]);
preb[x[i]]=y[i];
pos=lower_bound(all(id),preb[x[i]])-id.begin();
val=aid.sum(pos)*preb[x[i]];
val+=a.sum(q+1)-a.sum(pos);
ans+=val;
bid.add(pos,1);
b.add(pos,preb[x[i]]);
}
cout<<ans<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)
const int maxn = 2e5 + 5, inf = 1e9;
struct edge{int to; edge *nxt;} *arc[maxn], pool[maxn << 1], *pt = pool;
int n, k, len, cnt;
int mi[maxn], mx[maxn];
int getint()
{
char ch;
int res = 0, p;
while (!isdigit(ch = getchar()) && (ch ^ '-'));
p = ch == '-'? ch = getchar(), -1 : 1;
while (isdigit(ch))
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return res * p;
}
void dfs(int x, int fa)
{
mi[x] = inf; mx[x] = 0;
for (edge *e = arc[x]; e; e = e->nxt)
if (e->to != fa)
{
dfs(e->to, x);
mi[x] = min(mi[x], mi[e->to] + 1);
mx[x] = max(mx[x], mx[e->to] + 1);
}
if (mi[x] + mx[x] <= len)
mx[x] = -inf;
else if (mi[x] + mx[x] > len && mx[x] == len)
mi[x] = 0, mx[x] = -inf, cnt++;
}
bool check(int x)
{
len = x; cnt = 0;
dfs(1, 0);
cnt += (mi[1] + mx[1] > len);
return cnt <= k;
}
void addedge(int u, int v)
{
*pt = (edge){v, arc[u]};
arc[u] = pt++;
}
int main()
{
n = getint(); k = getint();
fo(i, 1, n - 1)
{
int u, v;
u = getint(); v = getint();
addedge(u, v);
addedge(v, u);
}
int l = 0, r = n + 1;
while (l + 1 < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid;
}
printf("%d\n", r);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i=0; i<(ll)(n); i++)
#define rev_rep(i, n) for (ll i=(n); i > 0; i--)
#define REP(i, n) for (ll i=1; i<=(ll)(n); i++)
#define FOR(i, n, m) for (ll i=(n); i<=(ll)(m); i++)
#define rev_FOR(i, n, m) for (ll i=(n); i<=(ll)(m); i--)
#define ll long long
#define vc vector<ll>
#define vc2 vector<vector<ll>>
#define vc3 vector<vector<vector<ll>>>
#define vcd vector<double>
#define vc2d vector<vector<double>>
#define vc3d vector<vector<vector<double>>>
#define vcs vector<string>
#define vc2s vector<vector<string>>
#define vc3s vector<vector<vector<string>>>
#define ALL(vec) vec.begin(),vec.end()
const ll INF=1LL<<60;
const ll mod = 1000000007;
const double pi=3.1415926535;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ox[4] = {1, -1, 1, -1};
const int oy[4] = {1, 1, -1, -1};
template<class T>bool ch_max(T &a, const T &b) {if (a<b) {a = b; return 1; } return 0;}
template<class T>bool ch_min(T &a, const T &b) {if (b<a) {a = b; return 1; } return 0;}
int CrossProduct(vc &a, vc &b, vc &c) {
return (a[0]-c[0])*(b[1]-c[1])-(a[1]-c[1])*(b[0]-c[0]);
}
int dist(vc &a, vc &b) {
return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]);
}
int main() {
int N;
cin >> N;
vc2 S(N, vc(2)),T(N, vc(2));
rep(i, N) cin >> S.at(i).at(0) >> S.at(i).at(1);
rep(i, N) cin >> T.at(i).at(0) >> T.at(i).at(1);
if(N == 1) {
cout << "Yes" << endl;
return 0;
}
else if(N == 2) {
if(dist(S.at(0), S.at(1)) == dist(T.at(0),T.at(1))) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
vc2 dS(N, vc(3));
rep(i, N) {
dS.at(i).at(0) = dist(S.at(0),S.at(i));
dS.at(i).at(1) = dist(S.at(1),S.at(i));
dS.at(i).at(2) = CrossProduct(S.at(0),S.at(1),S.at(i));
}
sort(ALL(dS));
rep(i, N) {
rep(j, N) {
if(i == j) continue;
vc2 dT(N, vc(3));
rep(k, N) {
dT.at(k).at(0) = dist(T.at(i), T.at(k));
dT.at(k).at(1) = dist(T.at(j), T.at(k));
dT.at(k).at(2) = CrossProduct(T.at(i), T.at(j), T.at(k));
}
sort(ALL(dT));
/*rep(k, N) {
cout << dT.at(k).at(0) << " ";
cout << dT.at(k).at(1) << " ";
cout << dT.at(k).at(2) << " ";
}
cout << endl;
rep(k, N) {
cout << dS.at(k).at(0) << " ";
cout << dS.at(k).at(1) << " ";
cout << dS.at(k).at(2) << " ";
}
cout << endl;
cout << endl;*/
if(dT == dS) {
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
} | #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main(){fastio
string s;cin>>s;
if(s[0]==s[1] && s[1]==s[2]){
cout<<"Won"<<endl;
}else{
cout<<"Lost"<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(long long int i=a; i<n; i++)
#define loop(i,a,n) for(long long int i=a; i>=n; i--)
int main()
{
ios_base :: sync_with_stdio(false),cin.tie(NULL);
long long int a, b, ans=0;
cin>>a>>b;
string s="";
forr(i, 0, a)
s+=(i+'0');
long long int c[a][a];
forr(i, 0, a)
{
forr(j, 0, a)
cin>>c[i][j];
}
do{
if(s[0]=='0')
{
int l=0, kk=0;
forr(i, 0, s.size())
{
kk+=c[l][s[i]-'0'];
l=s[i]-'0';
}
kk+=c[l][0];
//cout<<kk<<'\n';
if(kk==b)
ans++;
}
else
break;
}while(next_permutation(s.begin(), s.end()));
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main(){
int n,k;
cin >> n >> k;
vector<vector<int>> T(n, vector<int>(n));
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> T[i][j];
}
}
vector<int> index;
for(int i=0; i<n; i++)index.push_back(i);
int ans = 0;
do{
int time = 0;
for(int i=0; i<n; i++) time += T[index[i]][index[(i+1)%n]];
if(time == k) ans++;
}while(next_permutation(index.begin()+1, index.end()));
cout << ans << endl;
}
|
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll,ll>;
template <class T> using vv = vector<vector<T>>;
#define _roverload(_a1,_a2,_a3,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(...) _roverload(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);--i)
#define rrep(...) _roverload(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL<<(n))
template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void print() {cout << '\n';}
template<class T> void print(const vector<T> &v)
{bool f=false; for(const auto &e: v){if(f)cout << ' '; cout << e; f=true;} cout << '\n';}
template<class... T> void print(const T&... args)
{bool f=false; for(const auto &e: {args...}){if(f)cout << ' '; cout << e; f=true;} cout << '\n';}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess{preprocess()
{cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
#pragma endregion head
#pragma region library
#pragma endregion library
int main(){
string s; cin >> s;
int n = s.length();
vi cnt(26);
ll ans = 0;
rrep(i,n){
cnt[s[i]-'a']++;
if(i>0 && s[i-1]==s[i]){
rep(j,26){
if(s[i]-'a' == j) continue;
ans += cnt[j];
cnt[j] = 0;
}
cnt[s[i]-'a'] = n-i;
}
}
print(ans);
} | #include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#define ll long long
#define skip cin>>ws;
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define vpll vector<pair<ll,ll>>
#define vpi vector<pair<int, int>>
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define vvvll vector<vector<vector<ll>>>
#define pll pair<ll,ll>
#define vs vector<string>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvpi vector<vector<pair<int, int>>>
#define pb push_back
#define pob pop_back()
#define MOD (ll)(1e9 + 7)
#define MOD2 (ll)(998244353)
#define INF (ll)(1e18 + 5)
#define count1(n) __builtin_popcountll(n)
#define test ll t; cin>>t; while(t--)
#define enter(a) for(ll i=0;i<a.size();i++) cin>>a[i];
#define show(a) for(ll e: a) cout<<e<<" "; cout<<"\n";
using namespace std;
ll mo(ll a){ return a%MOD;}
ll po(ll x, ll y, ll p)
{
ll res = 1; x = x % p;
while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; }
return res%p;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
skip
cin>>s;
skip
ll n = s.size(), pre_in = -1, pre = -1, ans = 0;
vvll cnt(n + 1, vll(26));
cnt[n-1][s[n-1] - 'a']++;
for(ll i=n-2;i>=0;i--)
{
for(ll j=0;j<26;j++)
{
cnt[i][j] = cnt[i + 1][j];
}
cnt[i][s[i] - 'a']++;
if(s[i] == s[i + 1])
{
if(s[i] - 'a' == pre)
{
ans += max((pre_in - (i + 1) - 1), 0LL);
ans -= max((cnt[i + 1][s[i] - 'a'] - cnt[pre_in][s[i] - 'a'] -1), 0LL);
}
else
{
ans += (max((n-1) - (i + 1), 0LL) - (cnt[i + 2][s[i] - 'a']
- ((pre_in!=-1)?cnt[pre_in][s[i] - 'a'] :0LL)));
}
// cout<<ans<<"\n";
pre = s[i] - 'a';
pre_in = i;
}
}
cout<<ans<<"\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
long long ans = 0;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k / i; j++) {
for (int l = 1; l <= k / (i * j); l++) {
ans++;
}
}
}
cout << ans << endl;
return 0;
}
| // C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
LL g[202020], p[202020];
int main(){
// 1. 入力情報.
int K;
scanf("%d", &K);
// 2. 約数の個数を計算.
repx(i, 1, 202020) repex(j, i, 202020, i) g[j]++;
// 3. A * B <= K となる (A, B) の 個数 を 計算.
// f[1] = (1, 1)
// f[2] = f[1] に加えて, (1, 2), (2, 1)
// f[3] = f[2] に加えて, (1, 3), (3, 1)
// f[4] = f[3] に加えて, (1, 4), (2, 2), (4, 1)
// f[5] = f[4] に加えて, (1, 5), (5, 1)
// f[6] = f[5] に加えて, (1, 6), (2, 3), (3, 2), (6, 1)
// ...
//
repx(i, 1, 202020) p[i] = p[i - 1] + g[i];
// 4. A * B * C <= K となる (A, B, C) の 個数 を 計算.
LL ans = 0;
repx(i, 1, K + 1) ans += p[K / i];
// 5. 出力.
printf("%lld\n", ans);
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#include <numeric>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
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(a > b){ a = b; return 1; } return 0; }
template<class T> inline int sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
class Osa_k{
public:
int max_number;
vi min_factor;
Osa_k(int N) : max_number(N) {
Build();
}
void Build() {
min_factor.resize(max_number+1);
iota(min_factor.begin(),min_factor.end(), 0);
for(int i = 2; i <= max_number; ++i) {
if(min_factor[i] < i) continue;
for(int j = 1; i*j <= max_number; ++j) {
min_factor[i*j] = i;
}
}
}
vvi PrimeFactor() {
vvi res(max_number+1);
for(int i = 2; i <= max_number; ++i) {
int num = i;
while(num >= 2) {
int factor = min_factor[num];
res[i].push_back(factor);
while(num % factor == 0) num /= factor;
}
}
return res;
}
void Test() {
vvi res = PrimeFactor();
for(int i = 2; i <= max_number; ++i) {
cout << i << "\n";
for(auto tmp: res[i]) cout << tmp << " ";
cout << "\n";
}
}
};
int main()
{
ll l,r; cin >> l >> r;
Osa_k os(r+1);
vvi primeFactor = os.PrimeFactor();
ll res = 0;
for(int i = l; i <= r; ++i) {
int n = sz(primeFactor[i]);
rep1(S,(1<<n)-1){
int prod = 1;
rep(j,n) {
if((S>>j)&1) prod *= primeFactor[i][j];
}
ll count = r/prod - (i-1)/prod;
count -= r/i;
if(__builtin_popcount(S) % 2 == 1) res += count;
else res -= count;
}
}
cout << res*2 << "\n";
return 0;
}
| #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>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll,ll>
#define mp make_pair
# define fio ios_base::sync_with_stdio(false); cin.tie(NULL)
const int MAX_N = 2e6+1;
ll mod = 1e9+7;
int main()
{
fio;
int l,r;
cin>>l>>r;
ll store[r+1];
ll ans = 0;
for(int i=r;i>=2;i--){
store[i] =0;
for(int j=i;j<=r;j+=i){
store[i] -= store[j];
}
ll low = (l-1)/i;
ll high = r/i;
store[i] += (high-low) * (high-low);
ans += store[i];
}
for(int i=max(2,l);i<=r;i++){
ll high = r/i;
ans -= (2*high - 1);
}
cout<<ans;
return 0;
}
|
#include<iostream>
#define ll long long
using namespace std;
ll T,X,Y,P,Q,a,b,c,k1,k2,tans;
ll exgcd(ll a,ll b,ll &x,ll &y) {
if(!b) {
x=1,y=0;
return a;
}
int t=exgcd(b,a%b,y,x);
y-=a/b*x;
return t;
}
int main() {
cin>>T;
while(T--) {
cin>>X>>Y>>P>>Q;
a=2*X+2*Y,b=P+Q;
tans=5e18;
for(int q=0; q<Q; q++) {
for(int y=0; y<Y; y++) {
c=P+q-X-y;
int t=exgcd(a,b,k1,k2);
if(c%t)continue;
k1=k1*c/t;
k1=(k1%(b/t)+(b/t))%(b/t);
tans=min(tans,a*k1+y+X);
}
}
if(tans==5e18)cout<<"infinity\n";
else cout<<tans<<endl;
}
} | #include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
#define mul(a, b) ((a*b)%MOD)
#define add(a, b) ((a+b)%MOD)
#define dec(a, b) ((a-b+MOD)%MOD)
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int INF = 1e9;
const long LINF = 1e18;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
// inv[1] = 1;
// fac[0] = ifac[0] = 1;
// for(int i = 2; i <= 500000; i++) inv[i]=MOD-(MOD/i)*inv[MOD%i]%MOD;
// for(int i = 1; i <= 500000; i++) fac[i] = mul(i, fac[i-1]), ifac[i] = mul(inv[i], ifac[i-1]);
// /mnt/c/Users/vince/Desktop/POST-IOI/
map<int,int> cnt;
int A[100003];
int main()
{
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("input.in", "r", stdin);
int t; scanf("%d", &t);
while(t--)
{
int n; scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &A[i]);
}
if(n&1)
{
printf("Second\n");
}
else
{
cnt.clear();
for(int i = 1; i <= n; i++)
cnt[A[i]]++;
bool bol = 1;
for(auto it : cnt)
if(it.se&1)
bol = 0;
if(bol) printf("Second\n");
else printf("First\n");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a < b) {
for (int i = 1; i <= b; i++) {
cout << -i << " ";
}
for (int i = 1; i < a; i++) {
cout << i << " ";
}
a--;
cout << (b*(b+1)-a*(a+1))/2 << endl;
}
else {
for (int i = 1; i <= a; i++) {
cout << i << " ";
}
for (int i = 1; i < b; i++) {
cout << -i << " ";
}
b--;
cout << (b*(b+1)-a*(a+1))/2 << endl;
}
} | #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}
int main(){
INT(n);
int ans=n*108/100;
if(ans==206)out("so-so");
else if(ans>206)out(":(");
else out("Yay!");
} |
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<(a+b)/2<<" "<<(a-b)/2<<endl;
return 0;
} | /*ABIDSTIC*/
#include <bits/stdc++.h>
/*---------------------------------PB_DS---------------------------------*/
// #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>
// #define ordered_multiset tree<pair<int, int>, null_type,less<pair<int, int> >, rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(k) = returns an iterator to the k-th largest element (counting from zero)
// order_of_key(k) = the number of items in a set that are strictly smaller than k.
/*-----------------------------------------------------------------------*/
#define sf(a) scanf("%d",&a)
#define sfl(a) scanf("%lld",&a)
#define sff(a,b) scanf("%d %d",&a,&b)
#define sffl(a,b) scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define fill(a,b) memset(a, (b), sizeof(a))
#define debug(x) cout<<#x<<" "<<x<<'\n';
#define sfc(a) scanf("%c",&a)
#define pb(a) push_back(a)
#define X first
#define Y second
#define sz(x) x.size()
#define pii pair<int,int>
#define pli pair<long long int,int>
#define mp(a,b) make_pair(a,b)
#define fr(i,x,n) for(int i=x;i<n;i++)
#define rfr(i,x,n) for(int i=x;i>n;i--)
#define Lcm(a, b) ((a)*((b)/Gcd(a,b)))
#define endl '\n'
template<typename T>T Abs(T a){return a<0?-a:a;}
template<typename T>T Max(T a, T b){return a>b?a:b;}
template<typename T>T Min(T a, T b){return a<b?a:b;}
template<typename T>T Gcd(T a, T b){return (!b)?a:Gcd(b,a%b);}
template<typename T>T Pow(T a, T b){if(!b)return 1;T t=Pow(a,b/2);return b%2?a*t*t:t*t;}
template<typename T>inline void read(T &x){
T f=1;char c;x=0;
for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
x*=f;
}
using fl = float;
using db = double;
using ll = long long;
using ull = unsigned long long;
const int mx = 1000005;
const int inf = 99999999;
const int intlim = 2147483648;
const db PI = acos(-1); //3.14159265358979323846264338328
/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
/*---------------------------------------------------------------------*/
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//#endif
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int a,b;
cin>>a>>b;
int x=(a+b)/2;
int y=a-x;
cout<<x<<" "<<y<<endl;
return 0;
}
|
/**
* Made by: Diego Briaares
* At: 06.03.2021 16:16:56
**/
#include <bits/stdc++.h>
using namespace std;
void optimize() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
int main () {
optimize();
int n;
cin >> n;
vector<pair<int, int>> emp(n);
int ans = 1e9;
for (int i = 0; i < n; i++) {
cin >> emp[i].first >> emp[i].second;
ans = min(ans, emp[i].first + emp[i].second);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = min(ans, max(emp[i].first, emp[j].second));
ans = min(ans, max(emp[j].first, emp[i].second));
}
}
cout << ans << "\n";
} | #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//end of libraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int max_n = 300000;
using namespace std;
int n,m;
ll mod = 998244353 , fact[max_n];
ll binpow(ll a, ll b) {
a %= mod; ll res = 1;
while (b) { if (b%2) res = res * a % mod; a = a * a % mod; b /= 2; }
return res % mod;
}
void dofact() {
fact[0]=fact[1]=1;
rep(i,max_n-3){
if(i>1)fact[i] = fact[i-1]*i; fact[i] %= mod;
}
}
ll cnk(ll k , ll n){
if(k>n) return 0;
return (fact[n] * binpow(fact[k]*fact[n-k]%mod,mod-2)) % mod;
}
ll sub(ll a , ll b){
a%=mod; b%=mod;
return (((a-b)%mod)+mod)%mod;
}
ll prod(ll a , ll b){
a%=mod; b%=mod;
return ((a*b)%mod);
}
ll add(ll a , ll b){
a%=mod; b%=mod;
return (a+b)%mod;
}
int main(){
fcin;
dofact();
cin >> n >> m;
vector <pii> b;
ll ans = 0;
for(int i = 1 ; i <= m ; i++) {
int y = i;
b.clear();
for(int x = 2 ; x <= sqrt(i) ; x++) {
if(y%x == 0) {
int cnt = 0;
while(y%x == 0) {y/=x; cnt++;}
b.pb({x,cnt});
}
}
b.pb({y,1});
ll c = 1;
for(auto p : b) {
if(p.fi == 1) continue;
// cout << cnk(p.se,n+p.se-1) << " , ";
// c *= cnk(p.se,n+p.se-1);
c = prod(c,cnk(p.se,n+p.se-1));
}
// ans += c;
ans = add(ans,c);
// cout << c << "\n";
}
cout << ans << "\n";
/*
*/
return 0;
}
|
//selem
#include <bits/stdc++.h>
//void IO() {freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}
using namespace std;typedef long long ll;
#define all(x) begin(x), end(x)
#define pb push_back
typedef pair<ll,ll> pll;;typedef map<ll, ll> mll;
typedef vector<pll> vpll;
typedef vector<ll> vll;
int Arr[10000];vll adj[1000];
//finding root of an element.
int root(int Arr[ ],int i)
{
while(Arr[ i ] != i) //chase parent of current element until it reaches root.
{
i = Arr[ i ];
}
return i;
}
/*modified union function where we connect the elements by changing the root of one of the element */
int un(int Arr[ ] ,int A ,int B)
{
int root_A = root(Arr, A);
int root_B = root(Arr, B);
Arr[ root_A ] = root_B ; //setting parent of root(A) as root(B).
}
bool fnd(int A,int B)
{
if( root(Arr, A)==root(Arr, B) ) //if A and B have same root,means they are connected.
return true;
else
return false;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
double f;cin>>f;
double ff;cin>>ff;
cout<<(f/100.0)*ff;
return 0;
}
| #line 1 "main.cpp"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define repr(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define rep3r(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
long long bf[1 << 19];
long long bg[1 << 19];
void init() {
for (int i = 1; i <= 400000; i++) {
int pos = i;
while (pos % 3 == 0) { pos /= 3; bf[i] += 1; }
bg[i] = pos % 3;
}
bg[0] = 1;
for (int i = 1; i <= 400000; i++) bf[i] += bf[i - 1];
for (int i = 1; i <= 400000; i++) bg[i] = (bg[i] * bg[i - 1]) % 3;
}
int ncr_mod_3(int n, int r) {
if (bf[n] != bf[r] + bf[n - r]) return 0;
if (bg[n] == 1 && bg[r] * bg[n - r] == 1) return 1;
if (bg[n] == 1 && bg[r] * bg[n - r] == 2) return 2;
if (bg[n] == 1 && bg[r] * bg[n - r] == 4) return 1;
if (bg[n] == 2 && bg[r] * bg[n - r] == 1) return 2;
if (bg[n] == 2 && bg[r] * bg[n - r] == 2) return 1;
if (bg[n] == 2 && bg[r] * bg[n - r] == 4) return 2;
return -1;
}
map<char, int> c2d;
map<char, int> d2c;
int main() {
c2d['B'] = 0;
c2d['W'] = 1;
c2d['R'] = 2;
d2c[0] = 'B';
d2c[1] = 'W';
d2c[2] = 'R';
int n; cin >> n;
string s; cin >> s;
int ans = 0;
init();
rep(i, n) {
int v = c2d[s[i]];
// cerr << i << " " << ncr_mod_3(n-1,i) << " " << v << endl;
ans += ncr_mod_3(n-1,i)*v;
ans %= 3;
}
if (n % 2 == 0) ans *= 2;
ans %= 3;
// cerr << ans << endl;
cout << (char)d2c[ans] << endl;
return 0;
}
|
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define double long double
#define all(x) (x).begin(), (x).end()
#define RemoveDuplicates(a) a.resize(unique(all(a))-a.begin())
#define Debug(x) cerr<<"["<<#x<<": "<<x<<"]"
#define endl '\n'
void solve(){
int n,x;
cin>>n>>x;
vector<int>a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
map<int,int>cnt;
cnt[x]=1;
for(int i=0;i<n-1;i++){
map<int,int>newcnt;
int d=a[i+1]/a[i];
for(pair<int,int>p:cnt){
int val=p.first;
int count=p.second;
if(val%d!=0){
newcnt[val/d+1]+=count;
}
newcnt[val/d]+=count;
}
cnt=newcnt;
}
int ans=0;
for(pair<int,int>p:cnt){
ans+=p.second;
}
cout<<ans<<endl;
}
int32_t main(){
// Remove for Interactive Problems
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #define LOCAL // 提出時はコメントアウト
#define DEBUG_
typedef long long ll;
const double EPS = 1e-9;
const ll INF = ((1LL<<62)-(1LL<<31));
typedef vector<ll> vecl;
typedef pair<ll, ll> pairl;
template<typename T> using uset = unordered_set<T>;
template<typename T, typename U> using mapv = map<T,vector<U>>;
template<typename T, typename U> using umap = unordered_map<T,U>;
#define ALL(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define contains(S,x) find(ALL(S),x) != S.end()
ll llceil(ll a,ll b) { return (a+b-1)/b; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> vector<vector<T>> genarr(ll n, ll m, T init) { return vector<vector<T>>(n,vector<T>(m,init)); }
///// DEBUG
#define DUMPOUT cerr
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
template<typename T>istream&operator>>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<typename T,typename U>ostream&operator<<(ostream&os,pair<T,U>&pair_var){os<<"("<<pair_var.first<<", "<<pair_var.second<<")";return os;}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&vec){os<<"{";for(int i=0;i<vec.size();i++){os<<vec[i]<<(i+1==vec.size()?"":", ");}
os<<"}";return os;}
template<typename T,typename U>ostream&operator<<(ostream&os,map<T,U>&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;}
os<<"}";return os;}
template<typename T>ostream&operator<<(ostream&os,set<T>&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;}
os<<"}";return os;}
void dump_func(){DUMPOUT<<endl;}
template<class Head,class...Tail>void dump_func(Head&&head,Tail&&...tail){DUMPOUT<<head;if(sizeof...(Tail)>0){DUMPOUT<<", ";}
dump_func(std::move(tail)...);}
#ifndef LOCAL
#undef DEBUG_
#endif
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \
<< endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
//////////
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
vecl A;
vector<umap<ll,ll>> memo(60);
ll solve(ll x, int i) {
if (i >= A.size() - 1) return 1;
if (memo[i].count(x) > 0) return memo[i][x];
if (x % A[i+1] == 0) return memo[i][x] = solve(x, i+1);
return memo[i][x] = solve(x - x % A[i+1], i+1) + solve(x - x % A[i+1] + A[i+1], i+1);
}
int main() {
#ifdef LOCAL
ifstream in("../../Atcoder/input.txt");
cin.rdbuf(in.rdbuf());
#endif
ll N,X;
cin>>N>>X;
A.resize(N);
rep(i,N) cin>>A[i];
ll ans = solve(X,0);
cout << ans << endl;
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#define mod 1000000007
#define ll long long
using namespace std;
const int maxx=600010;
ll t,n,m;
ll ans=0,total=0,sum=0,cnt=0,l=1,r=1;
ll a[maxx],x[maxx],y[maxx];
ll bjl[3111][3111],bjr[3111][3111];
char c[3111][3111];
ll ksm(ll x,ll p)
{
ll res=1;
while(p)
{
if(p&1)res=res*x%mod;
x=x*x%mod;
p>>=1;
}
return res;
}
int main()
{
cin>>n>>m;
for(register int i=1;i<=n;i++)
for(register int j=1;j<=m;j++)
{
cin>>c[i][j];
if(c[i][j]=='.')total++;
}
for(register int i=1;i<=n;i++)c[i][m+1]='#';
for(register int i=1;i<=m;i++)c[n+1][i]='#';
for(register int i=1;i<=n+1;i++)
{
l=1;
for(register int j=1;j<=m+1;j++)
{
if(c[i][j]=='#')
{
for(register int k=l;k<j;k++)bjl[i][k]=x[i];
x[i]=0,l=j+1;
}
else x[i]++;
}
}
for(register int j=1;j<=m+1;j++)
{
r=1;
for(register int i=1;i<=n+1;i++)
{
if(c[i][j]=='#')
{
for(register int k=r;k<i;k++)bjr[k][j]=y[j];
r=i+1;
y[j]=0;
}
else y[j]++;
}
}
for(register int i=1;i<=n;i++)
for(register int j=1;j<=m;j++)
{
if(c[i][j]=='.')
{
sum=bjl[i][j]+bjr[i][j]-1;
ans=(ans+(ksm(2,sum)%mod-1+mod)%mod*ksm(2,total-sum)%mod)%mod;
}
}
printf("%lld",ans);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long int ll;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ull unsigned long long
#define d1(x) cout<<#x<<" "<<x<<endl;
#define d2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define d2i(x,y,i) cout<<#x<<i<<" "<<x<<" "<<#y<<i<<" "<<y<<endl;
#define fr(i,l,r) for(ll i=l;i<r;i++)
#define mems(a,x) memset(a,x,sizeof(a))
#define mod 1000000007
#define ff first
#define ss second
#define pb(x) push_back(x)
#define vll vector<ll>
#define pbp(x,y) push_back(make_pair(x,y))
#define all(v) v.begin(),v.end()
#define mat vector<vector<ll>>
#define el cout<<'\n';
const ll N=2e3+5;
ll lf[N][N];
ll rt[N][N],up[N][N],down[N][N];
ll pw(ll k){
ll a=2;
ll ans=1;
while(k){
if(k&1){
ans*=a;
ans%=mod;
}
a*=a;
a%=mod;
k>>=1;
}
return ans;
}
void solve(){
ll n,m,k;
cin>>n>>m;
int ar[n][m];
k=0;
fr(i,0,n){
fr(j,0,m){
char ch;
cin>>ch;
ar[i][j]=(ch=='.'?1:0);
k+=ar[i][j];
}
}
for(int i=0;i<n;i++){
fr(j,0,m){
if(j==0) lf[i][j]=ar[i][j];
else if(ar[i][j]==1)
lf[i][j]+=lf[i][j-1]+1;
if(i==0)
up[i][j]=ar[i][j];
else if(ar[i][j])
up[i][j]=up[i-1][j]+1;
}
}
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
if(j==m-1)
rt[i][j]=ar[i][j];
else if(ar[i][j]==1)
rt[i][j]=rt[i][j+1]+1;
if(i==n-1)
down[i][j]=ar[i][j];
else if(ar[i][j]==1)
down[i][j]=down[i+1][j]+1;
}
}
// d1(k)
ll ans=k*pw(k)%mod;
ll ng=0;
fr(i,0,n){
fr(j,0,m){
if(ar[i][j]==1){
ll l=0,r=0,u=0,d=0;
if(j!=0)
l=lf[i][j-1];
if(j!=m-1)
r=rt[i][j+1];
if(i!=0)
u=up[i-1][j];
if(i!=n-1)
d=down[i+1][j];
ll tot=l+r+u+d+1;
// d1(tot);
ng+=pw(k-tot);
ng%=mod;
}
}
}
cout<<(ans-ng+mod)%mod;
}
int main(){
boost
ll t=1;
// cin>>t;
while(t--){
solve();
el
}
return 0;
}
|
/*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(n);
V<ll> a(n);
in1(a);
V<ll> ans;
rep(i,n)a[i]--;
ll now=0;
ll old=0;
while(now<n){
if(now==n-1 || a[now]==old){
for(int i=now-1;i>=old;i--){
swap(a[i],a[i+1]);
//outv(a);
ans.push_back(i);
}
old=now;
}
now++;
}
rep(i,n){
if(a[i]!=i)die(-1);
}
rep(i,ans.size())out(ans[i]+1);
return 0;
} | #include <bits/stdc++.h>
using int32 = int;
using int64 = long long;
using namespace std;
class Solution
{
#define int int64
#define sfor(i, n) for (int i = 1; i <= n; ++i)
#define tfor(i, n) for (int i = 0; i < n; ++i)
int x, y, r, res = 0;
void SHURU()
{
double a, b, c;
cin >> a >> b >> c;
x = round(a * 10000);
y = round(b * 10000);
r = round(c * 10000);
}
void SHUCHU()
{
cout << res << endl;
}
bool inCircle(int dx, int dy)
{
return dx * dx + dy * dy <= r * r;
}
void CHULI()
{
x %= 10000;
y %= 10000;
int l = 0, r = 1;
for (int i = 1000100000; i >= 0; i -= 10000)
{
while (inCircle(x - l * 10000, y + i))
--l;
while (inCircle(x - r * 10000, y + i))
++r;
res += r - l - 1;
}
l = 0;
r = 1;
for (int i = 1000100000; i >= 10000; i -= 10000)
{
while (inCircle(x - l * 10000, y - i))
--l;
while (inCircle(x - r * 10000, y - i))
++r;
res += r - l - 1;
}
}
public:
Solution()
{
SHURU();
CHULI();
SHUCHU();
}
#undef int
#undef tfor
#undef sfor
};
int32 main()
{
#ifdef LILY
while (1)
{
#endif
Solution();
#ifdef LILY
}
#endif
return 0;
}
|
#include<iostream>
using namespace std;
int main(){
int a,b,x,y,ans;
cin>>a>>b>>x>>y;
if(a>b)
if(2*x<y)
ans=2*x*(a-b)-x;
else
ans=x+y*(a-b-1);
else
if(2*x<y)
ans=2*x*(b-a)+x;
else
ans=x+y*(b-a);
cout<<ans;
} | #include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;
signed main(){
ll a, b, x, y;
cin >> a >> b >> x >> y;
ll t = 0;
if(a == b){
PRINT(x);
}else if(a > b){
t += (a - b - 1) * min(2 * x, y);
t += x;
PRINT(t);
}else{
t += (b - a) * min(2 * x, y);
t += x;
PRINT(t);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define int long long
const int N = (int) 2e6 + 7;
int n, k, a[N];
vector<int> p[N];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 0; i <= n; i++) p[i].push_back(0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]].push_back(i);
}
for (int i = 0; i <= n; i++) p[i].push_back(n + 1);
for (int x = 0; x <= n; x++) {
for (int i = 0; i + 1 < (int) p[x].size(); i++) {
int space = p[x][i + 1] - p[x][i] - 1;
if (space >= k) {
cout << x << "\n";
return 0;
}
}
}
assert(0);
}
| #include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
#define ll long long
#define db double
template<typename T>
inline void read(T&x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
ll n,k;
inline ll calc(ll x){
if(x<2||x>2*n)return 0;
return min(n,x-1)-max(1ll,x-n)+1;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
r(n),r(k);
ll ret=0;
for(int i=2;i<=2*n;++i){
ret+=calc(i)*calc(i-k);
}
printf("%lld\n",ret);
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int64_t MOD=1000000007;
const long long INF = 1LL<<60;
void YN(bool x){
if(x) cout<<"Yes";
else cout<<"No";
}
int main() {
bool f=false;
string S;cin>>S;
int s=S.size();
vector<int> C(10,0);
rep(i,s) C[S[i]-'0']++;
for(int i=0;i<=1000;i+=8){
vector<int> t(10,0);
t[i%10]++;
t[(i/10)%10]++;
t[(i/100)%10]++;
bool k=true;
rep(j,10){
if(C[j]<t[j])k=false;
}
if(k)f=true;
}
if(s==1){
if(S[0]=='8')f=true;
else f=false;
}
if(s==2){
int p=10*(S[0]-'0')+(S[1]-'0'),q=10*(S[1]-'0')+(S[0]-'0');
if(p%8==0||q%8==0)f=true;
else f=false;
}
YN(f);
} | //#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
if ((tmp & ma0) ^ ma0) {
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += (72 - dig >> 3);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
if (*ci++ == '0') {
tmp = tmp * 10;
ci++;
}
}
}
return tmp;
}
const int MAX = 100000;
class shuturyoku_unko {
public:
char C[MAX * 5];
constexpr shuturyoku_unko() : C() {
rep(i, MAX) {
int X = i;
rep(j, 5) {
C[i * 5 + 4 - j] = '0' + X % 10;
X /= 10;
}
}
}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char dn[dm], * di = dn, * owad = dn + dm - 20;
void putint(ll A) {
if (owad < di) {
fwrite(dn, 1, di - dn, stdout);
di = dn;
}
int dig = 1;
if (A >= 100000) {
if (A >= 1000000000) dig = 5;
else if (A >= 100000000) dig = 4;
else if (A >= 10000000) dig = 3;
else if (A >= 1000000) dig = 2;
memcpy(di + dig, f.C + A % 100000 * 5, 5);
A /= 100000;
memcpy(di, f.C + A * 5 + 5 - dig, dig);
di += 5 + dig;
}
else {
if (A >= 10000) dig = 5;
else if (A >= 1000) dig = 4;
else if (A >= 100) dig = 3;
else if (A >= 10) dig = 2;
memcpy(di, f.C + A * 5 + 5 - dig, dig);
di += dig;
}
*di++ = '\n';
}
const int mod = 1e9 + 7;
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int T = getint();
rep(t, T) {
ll N = getint(), A = getint(), B = getint();
ll a = min(A, N - B + 1);
ll b = min(A + B - 1, N - B + 1);
ll are = (a + b) * (b - a + 1) + b * (N - A + 1 - (b - a + 1) * 2);
ll atti = (N - A + 1) * (N - B + 1);
ll kotae = (atti + are) % mod * ((atti - are) % mod) % mod;
putint(kotae);
}
fwrite(dn, 1, di - dn, stdout);
Would you please return 0;
} |
#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph= vector<vector<int>>;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define fi first
#define se second
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
// ミント
const ll mod = //1e9+7 ;
998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//昆布
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(int n,int k){
return fact[n]*ifact[n-k] ;
}
} ;
mint modpow(ll a,ll b){
if(b==0) return 1 ;
mint c= modpow(a,b/2) ;
if(b%2==1) return c*c*a ;
else return c*c ;
}
mint komb(ll n,ll m){
mint x=1 ;mint y=1 ;
rep(i,m){
x*= n-i ;
y*= i+1 ;
}
return x/y ;
}
map<ll,ll> factor(ll n){ //素因数とオーダーをマップで管理
map <ll,ll> ord ;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
int res=0;
while(n%i==0){
n/=i;
res++;
}
ord[i]=res;
}
}
if(n!=1) ord[n]++;
return ord ;
}
///
template< typename T >
T extgcd(T a, T b, T &x, T &y) {
T d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
////
struct UnionFind {
vector<int> d;
UnionFind(int n=0): d(n,-1) {}
int find(int x) {
if (d[x] < 0) return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y);}
int size(int x) { return -d[find(x)];}
};
mint dp[3005][3005] ;
mint rui[3005][3005] ;
int main(){
ll n,k ; cin>>n>>k ;
rep2(i,1,3000) dp[i][i]=1 ;
rep2(i,1,n-k){
rep2(j,1,i-1){
mint now=0ll ;
//rep2(qq,1,min(2*j,i-j)) now+= dp[i-j][qq] ;
now= rui[i-j][min(2*j,i-j)] ;
dp[i][j]=now ;
}
rep2(ww,1,i) rui[i][ww]=rui[i][ww-1]+dp[i][ww] ;
}
mint ans= 0 ;
rep2(i,1,min(n-k,k)) ans+= dp[n-k][i] ;
if((n-k)==0) cout<<1<<endl ;
else cout<<ans<<endl ;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr char newl = '\n';
ll dp[100][200000 + 10];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, K;
ll m;
cin >> n >> K >> m;
int lim_sum = (n - 1) / 2;
lim_sum = lim_sum * (lim_sum + 1) / 2 * K;
dp[0][0] = 1;
for (int i = 1; i < n; i++) {
int max_sum = min((i - 1) * i / 2 * K, lim_sum);
for (int j = 0; j <= max_sum; j++) {
if (dp[i - 1][j] == 0) continue;
for (int k = 0; k <= K; k++) {
if (j + k * i > lim_sum) break;
dp[i][j + k * i] += dp[i - 1][j];
if (dp[i][j + k * i] >= m) dp[i][j + k * i] -= m;
}
}
}
for (int i = 1; i <= n; i++) {
int li = i - 1;
int ri = n - i;
int mini = min(li, ri);
int max_sum = mini * (mini + 1) / 2 * K;
ll ans = 0;
for (int j = 0; j <= max_sum; j++) {
(ans += dp[li][j] * dp[ri][j] % m);
if (ans >= m) ans -= m;
}
ans = (ans * (K + 1) + m - 1) % m;
cout << ans << newl;
}
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
struct qwe{
int t,i;
}a[1005],b[1005];
int n;
bool cmp(qwe a,qwe b){
return a.t<b.t;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i].t>>b[i].t,a[i].i=b[i].i=i;;
sort(a,a+n,cmp);
sort(b,b+n,cmp);
if(a[0].i!=b[0].i) cout<<max(a[0].t,b[0].t);
else cout<<min(min(max(a[1].t,b[0].t),max(a[0].t,b[1].t)),a[0].t+b[0].t);
}
| #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define LL long long
#define ULL unsigned long long
const LL INF = 1LL<<62;
const double PI = 2.0 * acos(0.0);
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
////// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//
//typedef tree<
//LL,
//null_type,
//less< pll >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout)
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define sf scanf
#define pf printf
#define F first
#define S second
#define pb push_back
#define NL pf("\n")
#define ALL(v) v.begin(),v.end()
#define si(x) sf("%d",&x);
#define sii(x,y) sf("%d%d",&x,&y);
#define siii(x,y,z) sf("%d%d%d",&x,&y,&z);
#define sl(x) sf("%lld",&x);
#define sll(x,y) sf("%lld%lld",&x,&y);
#define slll(x,y,z) sf("%lld%lld%lld",&x,&y,&z);
#define sqr(x) ( (x) * (x) )
#define mem(x,y) memset(x,y,sizeof x )
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define deb(x) cerr<<#x<<" :: "<<x<<"\n"
#define SV(v) sort(v.begin(),v.end())
#define SVR(v,cmp) sort(v.begin(),v.end(),cmp)
#define FOR(i,n) for(int i=0;i<n;i++)
#define cnd tree[idx]
#define lc tree[idx*2]
#define rc tree[idx*2+1]
#define lnd (2*idx),(b),( (b+e) /2 )
#define rnd ((2*idx)+1),(((b+e)/2)+1),(e)
// graph direction array [4]
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int BX[8]={0,0,1,-1,-1,1,1,-1};
//int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};
// Bit Operations
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));} // check bit
inline int setBit(int N,int pos){ return N=N | (1<<pos);}// set bit
inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}// unset bit
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }// toggle bit
inline LL modMul(LL a, LL b,LL mod){
LL ans = 0;
a = a % mod;
while (b > 0){
if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
a = (a%mod * 2%mod) % mod;
b /= 2;
}
return ans % mod;
}
inline LL powerMod(LL a,LL b,LL mod){
if(b==0)return 1LL%mod;
LL x=powerMod( a,b/2,mod );
x = ( x%mod * x%mod ) % mod;
if( b%2==1 )x = ( x%mod * a%mod ) % mod;
return x%mod;
}
inline LL lcm( LL a,LL b ){
LL g = __gcd(a,b);
return ((a/g) * b);
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 2234567;
int A[1005],B[1005];
void solve(){
int n,m,k;
int x = INT_MAX,y = INT_MAX,z = INT_MAX;
cin>>n;
FOR(i,n){
int a,b;
cin>>a>>b;
A[i] = a;
B[i] = b;
z = min(a+b,z);
}
int ans = z;
FOR(i,n){
FOR(j,n){
if(i==j)continue;
ans = min(ans,max(A[i],B[j]));
}
}
cout<<ans<<endl;
}
int main(){
BOOST;
#ifdef MujahidPC
double start = clock();
// READ;
// WRITE;
#endif
int t;
t = 1;
// cin>>t;
while(t--)solve();
#ifdef MujahidPC
double tim = (clock() - start)/CLOCKS_PER_SEC;
cerr<<"Running Time : "<<tim<<" \n";
#endif
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
using namespace std;
const LL MOD=1e9+7;
int n,m,a[20][20],ok[300000],dp[300000];
int main()
{
cin>>n>>m;
int x,y;
rep(i,m)
{
scanf("%d%d",&x,&y);
--x;
--y;
a[x][y]=a[y][x]=1;
}
rep(i,1<<n)
{
bool b=true;
rep(j,n)
{
for(int k=j+1;k<n;k++)
{
if((i&(1<<j))==0||(i&(1<<k))==0) continue;
if(a[j][k]==0)
{
b=false;
break;
}
}
if(!b) break;
}
ok[i]=(int)b;
}
rep(i,(1<<n)+5) dp[i]=1e9;
dp[0]=0;
rep(i,1<<n)
{
if(dp[i]==1e9) continue;
int lft=(1<<n)-1-i;
for(int sub=lft;sub>0;sub=(sub-1)&lft) if(ok[sub]==1) dp[i|sub]=min(dp[i|sub],dp[i]+1);
}
cout<<dp[(1<<n)-1]<<endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::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...);
}
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
const int N=0;
char win(char a,char b){
if(a==b)return a;
if(a=='R'){
if(b=='P')return b;
else return a;
}
if(a=='P'){
if(b=='S')return b;
else return a;
}
if(a=='S'){
if(b=='R')return b;
else return a;
}
return a;
}
void solve(){
int n,k;
cin>>n>>k;
string s;
cin>>s;
for (int i = 0; i < k; ++i) {
if(sz(s)%2){
s+=s;
}
string t = "";
for (int j = 0; j < sz(s); j+=2) {
t+=win(s[j],s[j+1]);
}
s=t;
}
cout<<s[0];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define Y cout<<"YES"<<endl;
#define N cout<<"NO"<<endl;
#define tc int t; cin>>t; while(t--)
#define eki int n; cin>>n;
#define loop for(int i=0; i<n; i++)
#define mem(a,b) memset(a, b, sizeof(a) )
const int mx=1e4+123;
int main(){
int a, b, c;
cin>>a>>b>>c;
if((a+c-2*b)==0 || (b+c-2*a)==0 || (a+b-2*c)==0)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
| /*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*/
#include <iostream>
using namespace std;
int main()
{
int arr[3];
for(int i=0; i<3; i++)
{
cin>>arr[i];
}
if(arr[0] == arr[1] && arr[1] == arr[2]){
cout<<"Yes"<<endl;
return 0;
}
if((arr[0]+arr[1] == 2*arr[2]) || (arr[0]+arr[2] == 2*arr[1]) || (arr[1]+arr[2] == 2*arr[0]))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<ll,ll>
#define pll pair<ll,pl>
#define pi pair<int,int>
ll mod = 1e9+7;
const int MAXM = 200050;
void solve(int test)
{
string s;
cin>>s;
if(s[0] == s[1] && s[1] == s[2])
cout<<"Won\n";
else
cout<<"Lost\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
//cin>>t;
t = 1;
for(int i=1;i<=t;i++)
solve(i);
} | #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i)
#define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME
#define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__)
#define RREP2(i, n) for (int i = (n - 1); i >= 0; --i)
#define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i)
#define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME
#define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__)
#define IN(type, n) type n; cin >> n
#define INALL(v) for (auto &e : v) { cin >> e; }
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl
#else
#define DEBUG(x)
#endif
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
#pragma endregion
int main() {
IN(int, X);
IN(int, Y);
int mi = min(X, Y);
int ma = max(X, Y);
if (mi + 3 > ma) yes();
else no();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0)
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second
const int N=2002;
char a[N][N];
int hor[N][N],ver[N][N],sum,mod=1e9+7,pwr[4000005];
int32_t main(){
ios;
int n,m;
cin>>n>>m;
pwr[0]=1;
F(i,1,4000000) pwr[i]=pwr[i-1]*2%mod;
F(i,1,n){
F(j,1,m){
cin>>a[i][j];
sum+=(a[i][j]=='.');
}
}
//precalculation
F(i,1,n){
int prv=0;
F(j,1,m){
if(a[i][j]=='#') prv=j;
else hor[i][j]=j-prv-1;
}
prv=m+1;
R(j,m,1){
if(a[i][j]=='#') prv=j;
else hor[i][j]+=prv-j-1;
}
}
F(j,1,m){
int prv=0;
F(i,1,n){
if(a[i][j]=='#') prv=i;
else ver[i][j]=i-prv-1;
}
prv=n+1;
R(i,n,1){
if(a[i][j]=='#') prv=i;
else ver[i][j]+=prv-i-1;
}
}
//calculation
int ans=0;
F(i,1,n){
F(j,1,m){
if(a[i][j]=='#') continue;
int tol=hor[i][j]+ver[i][j]+1;
int aa=(pwr[tol]-1+mod)%mod;
int bb=pwr[sum-tol];
ans+=aa*bb%mod;
ans%=mod;
}
}
cout<<ans<<endl;
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const int MOD = 998244353;
const int MX = (int)1e5 + 10;
void add_self(int &x, int y, int md = MOD){x += y; if(x >= md) x -= md;}
void sub_self(int &x, int y, int md = MOD){x -= y; if(x < 0) x += md;}
void mul_self(int &x, int y, int md = MOD){x = x * 1LL * y % md;}
inline int mul(int x, int y, int md = MOD){return x * 1LL * y % md;}
inline int add(int x, int y, int md = MOD){x += y; if(x >= md) x -= md; return x;}
inline int sub(int x, int y, int md = MOD){x -= y; if(x < 0) x += md; return x;}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
int bp(int x, int exp){
int res = 1;
while(exp){
if(exp & 1) mul_self(res, x);
mul_self(x, x);
exp >>= 1;
}
return res;
}
int inv(int x){
return bp(x, MOD - 2);
}
void solve(){
int n, m;
cin >> n >> m;
vector<int> vec(m);
for(auto &x : vec) cin >> x;
vec.push_back(0);
vec.push_back(n + 1);
sort(all(vec));
int mn = n;
vector<int> df;
for(int i = 1; i < m + 2; ++i){
int x = vec[i] - vec[i - 1] - 1;
if(x > 0){
mn = min(mn, x);
df.push_back(x);
}
}
assert(mn > 0);
int res = 0;
for(auto &x : df){
res += (x + mn - 1) / mn;
}
cout << res << '\n';
}
int main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(0);
// freopen("tree.in", "r", stdin);
// freopen("output.out", "w", stdout);
int t;
t = 1;
// cin >> t;
// pre();
for(int i = 1; i <= t; ++i){
// cout << "Case #" << i << ": ";
solve();
// cout << '\n';
// cout << endl;
}
return 0;
} |
#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 range(i, a, b) for (ll i = (a); i < (b); 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 };
ll mul(ll a, ll b) { return (a > INF / b ? INF : a * b); }
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_;
const int N = 200010;
vector<vector<int>> graph(N, vector<int>());
vector<int> xd(N), yd(N);
void dfs(int n, int p, int d, vector<int> &v) {
v[n] = d;
for (int i : graph[n]) {
if (i != p) dfs(i, n, d + 1, v);
}
}
vector<int> ans(N);
void tour(int n, int p, vector<int> &v) {
v.pb(n);
for (int i : graph[n]) {
if (i != p && ans[i] == 0) {
tour(i, n, v);
v.pb(n);
}
}
}
int main() {
int n;
cin >> n;
rep(i, n - 1) {
int u, v;
cin >> u >> v;
u--; v--;
graph[u].pb(v);
graph[v].pb(u);
}
dfs(0, -1, 0, xd);
int ver = -1; int m = 0;
rep(i, n) if (chmax(m, xd[i])) ver = i;
dfs(ver, -1, 0, xd); int rver = 0; m = 0;
rep(i, n) if (chmax(m, xd[i])) rver = i;
dfs(rver, -1, 0, yd);
rep(i, n) {
int r = -1;
vector<int> nv;
for (int nxt : graph[i]) {
if (yd[nxt] < yd[i]) {
r = nxt;
}
else nv.pb(nxt);
}
if (r != -1) nv.pb(r);
graph[i] = nv;
}
vector<int> vec;
tour(ver, -1, vec);
rep(i, vec.size()) {
if (ans[vec[i]] == 0) ans[vec[i]] = i + 1;
}
rep(i, n) cout << ans[i] << ' ';
cout << '\n';
} |
//Shrey Dubey
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<unordered_map>
#include<vector>
#include<set>
#include<list>
#include<iomanip>
#include<queue>
#include<stack>
#include <math.h>
#include<climits>
#include<bitset>
#include<cstring>
#include<numeric>
#include<array>
using namespace std;
typedef long long ll;
typedef long double ld;
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define prDouble(x) cout<<fixed<<setprecision(10)<<x //to print decimal numbers
#define pb push_back
#define ff first
#define sec second
#define umap unordered_map
#define mp make_pair
#define KOBE ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fo(n) for(ll i = 0; i<n; i++)
#define fnd(stl, data) find(stl.begin(), stl.end(), data)
#define forn(x,n) for(ll x = 0; x<n; x++)
#define imax INT_MAX
#define lmax LLONG_MAX
#define imin INT_MIN
#define lmin LLONG_MIN
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pair<ll,ll> >
#define vb vector<bool>
#define pr(t) cout<<t<<"\n"
#define int long long
#define ql queue<ll>
#define qp queue<pair<ll,ll> >
#define endl "\n"
#define nl cout<<"\n"
#define re cin >>
#define pll pair<ll,ll>
#define FOR(a,b) for(ll i = a; i<=b; i++)
#define all(x) x.begin(),x.end()
// ll dx[] = {1,0,-1,0};
// ll dy[] = {0,1,0,-1};
ll mod = 1e9 + 7;
ll cl(ld a){
if(a>(ll) a){
return (ll)a+1;
}
else{
return (ll)a;
}
}
ll flr(ld a){
if(a < 0.0){
return (ll) a - 1;
}
return (ll) a;
}
ll mx = 1e18;
map<ll,ll> dp;
ll recur(ll x, ll y){
if(x == y) return 0;
if(x >= y) return x-y;
if(dp.count(y)){
return dp.at(y);
}
if(y == 1) return 1;
// cout<<x<<"()"<<y<<"\n";
ll ans;
if(y%2 == 1){
ans = 1 + min(recur(x,y+1),recur(x,y-1));
}
else{
ans = 1 + min(recur(x,y/2),y-x-1);
}
dp[y] = ans;
return ans;
}
//code starts here
void solve(){
ll x,y; re x; re y;
pr(recur(x,y));
}
int32_t main(){
KOBE;
ll t;
t = 1;
// re t;
while(t--) solve();
}
//common errors
// row - n, col - m always and loop var
// see the freq of numbers carefully
// see if there's array overflow
// use map for large inputs
//problem ideas
//check piegonhole wherever possible
//there might be many instances of limited answers like 0,1,2 only
// see suffix and prefix
//don't be obsessed with binary search
// try to find repeating pattern in matrices
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MS(x) memset((x), -1, sizeof((x)))
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 MOD 998244353
template<int64_t mod> struct Combination{
vector<int64_t> f, rf;
Combination(int max_n): f(max_n+1), rf(max_n+1){
f[0] = 1;
for(int i = 1;i <= max_n;i++) f[i] = f[i-1] * i % mod;
int64_t a = f[max_n], inv = 1;
for(int64_t b = mod-2;b > 0;b>>=1){
if(b & 1) inv = inv*a % mod;
a = a * a % mod;
}
for(int i = max_n;i >= 0;i--) inv = (rf[i] = inv) * i % mod;
}
int64_t operator()(int n, int r){
return (r < 0 || n < r) ? 0 : f[n] * rf[r] % mod * rf[n-r] % mod;
}
};
int n, m;
Combination<MOD> ncr(200010);
int memo[50][200010];
int dp(int i, int j){
if(i > n) return 0;
if(memo[i][j] != -1) return memo[i][j];
int ans = ncr((n-i)+(i-1), i-1);
for(int k = j*2;k <= m;k+=j){
ans += dp(i+1, k);
ans %= MOD;
}
return memo[i][j] = ans;
}
signed main(){
cin >> n >> m;
MS(memo);
int ans = 0;
for(int i = 1;i <= m;i++){
ans += dp(1, i);
ans %= MOD;
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <iomanip>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;
ll po(ll a, ll b) {
ll c = 1;
while (b) {
if (b % 2) c = c * a % 998244353;
a = a * a % 998244353;
b >>= 1;
}
return c;
}
int main() {
ios::sync_with_stdio(false);
//110880 144 166320 160
//H(n,j)=C(n+j-1,j)
//_1_2_3_4_5
int n, m;
ll z = 0, f[1000006] = {1}, g[1000006] = {};
for (int i = 1; i <= 1000000; i++) f[i] = f[i - 1] * i % 998244353;
for (int i = 0; i <= 1000000; i++) g[i] = po(f[i], 998244351);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
vector<int> a;
int b = i;
ll c = 1;
for (int j = 2; j * j <= i; j++) if (b % j == 0) {
a.push_back(0);
while (b % j == 0) b /= j, a.back()++;
}
if (b > 1) a.push_back(1);
for (int j : a) {
c = c * f[n + j - 1] % 998244353 * g[j] % 998244353 * g[n - 1] % 998244353;
}
z = (z + c) % 998244353;
}
cout << z << '\n';
}
|
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define gp __gnu_pbds
#define iset(T) gp::tree<T,gp::null_type,less<T>,gp::rb_tree_tag,gp::tree_order_statistics_node_update>
using namespace std;
#define SQ(x) ((x)*(x))
#define u unsigned
#define ull unsigned long long
#define ll long long
#define ld long double
#define test(x) cout<<"here "<<x<<endl;
#define debug(x) cout<<(#x)<<": "<<(x)<<endl;
#define inputstl(v) for(auto& x:v) cin>>x;
#define input(arr,n) for(ull i=0;i<n;++i){cin>>arr[i];}
#define print(arr,n) for(ull i=0;i<n;++i){cout<<arr[i]<<" ";}cout<<'\n';
#define printall(x) for(auto y:x){cout<<y<<' ';}cout<<'\n';
#define printallpii(x) for(auto y:x){cout<<y.fi<<','<<y.se<<' ';}cout<<'\n';
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define mod (ll)(1e9+7)
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define SENSITIVITY 1e-9
#define EQUAL(x1,x2) [](auto x1,auto x2){double ep=0.0001;double pos=x1-x2;if(pos<0){pos=-pos;}double maxi=(x1>x2)?(x1):(x2);if(pos/(maxi*ep)<SENSITIVITY)return true;else return false;}(x1,x2)
bool isPrime(ll a)
{
ll lim=sqrt(a);
lim++;
if(a==1)
return false;
for(ll i=2; i<=lim; ++i)
{
if(a%i==0)
{
return false;
}
}
return true;
}
bool isPali(string s)
{
int _size=(s.length())/2;
for(int i=0; i<_size; ++i)
{
if(s.at(i)!=s.at(s.length()-1-i))
{
return false;
}
}
return true;
}
int gcd(int a,int b)
{
if(a==0)
{
return b;
}
else if(b==0)
{
return a;
}
else
{
int shift=__builtin_ctz(a|b);
a>>=__builtin_ctz(a);
do
{
b>>=__builtin_ctz(b);
if(a>b)
{
a=a^b;
b=a^b;
a=a^b;
}
b-=a;
}
while(b);
return a<<shift;
}
}
ll gcdll(ll a,ll b)
{
if(a==0)
{
return b;
}
else if(b==0)
{
return a;
}
else
{
ll shift=__builtin_ctzll(a|b);
a>>=__builtin_ctzll(a);
do
{
b>>=__builtin_ctzll(b);
if(a>b)
{
a=a^b;
b=a^b;
a=a^b;
}
b-=a;
}
while(b);
return a<<shift;
}
}
ll power(ll a,ll b,ll _mod)
{
ll temp=a;
ll ans=1;
int limit=63-__builtin_clzll(b);
int index=0;
while(index<=limit)
{
if(b&(1ll<<index))
{
ans=(ans*temp)%_mod;
}
++index;
temp=(temp*temp)%_mod;
}
return ans;
}
int main()
{
fastio;
int tt=1;
// cin>>tt;
while(tt--)
{
int n;
cin>>n;
vector<ll> a(n),b(n);
inputstl(a);
inputstl(b);
ll sum=0;
for(int i=0;i<n;++i)
{
sum+=(a[i]*b[i]);
}
if(sum==0)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
| #include<iostream>
using namespace std;
int n,a[100020],b[100020],s=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
s+=a[i]*b[i];
}
cout<<(s?"No":"Yes")<<endl;
return 0;
} |
// ______ ______ ______
// .-----..-----..-----..-----.| __||__ || __ |
// | || _ || || _ || __ || __|| __ |
// |__|__||___ ||__|__||___ ||______||______||______|
// |_____| |_____|
//
# include <bits/stdc++.h>
# 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 step(n) rep(_, n)
# 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 contains(x,key) ((x).find(key) != (x).end())
# define cauto const auto
# define int long long
# define float long double
# define FI first
# define SE second
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
using mii = map<int, int>;
using msi = map<string, int>;
template<class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& x : v) is >> x; return is; }
template<class T> istream& operator>>(istream& is, vector<vector<T>>& v) { for(auto& x : v) for(auto& y : x) is >> y; return is; }
template<class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.FI >> p.SE; }
template<class T> string join(const vector<T> &v){ stringstream s; rep (i, len(v)) s<<' '<<v[i]; return s.str().substr(1); }
template<class T> ostream& operator<<(ostream& os, const vector<T>& v){ if (len(v)) os << join(v); return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){ rep (i, len(v)) { if (len(v[i])) os << join(v[i]) << (i-len(v)+1 ? "\n" : ""); } 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...);}
template<class T, class U> ostream& operator<<(ostream& os, const pair<T,U>& p){ cout << p.FI << " " << p.SE; return os; }
template<class T> inline void Unique(T& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }
template<class T> inline constexpr bool chmax(T &a, T b) { return a < b && (a = b, true); }
template<class T> inline constexpr bool chmin(T &a, T b) { return a > b && (a = b, true); }
constexpr int ctoi(const char c) { return ('0' <= c && c <= '9') ? (c - '0') : -1; }
const char* YesNo(bool b) { return b ? "Yes" : "No"; }
const char* YESNO(bool b) { return b ? "YES" : "NO"; }
const char* yesno(bool b) { return b ? "yes" : "no"; }
const char* yn(bool b) { return YesNo(b); }
int32_t main() {
int n;
cin >> n;
vector<int> imos(1000000 + 1 + 1);
rep (i, n) {
int a, b;
cin >> a >> b;
imos[a]++;
imos[b + 1]--;
}
rep (i, len(imos)) imos[i+1] += imos[i];
int ans = 0;
rep (i, len(imos)) ans += imos[i] * i;
print(ans);
return 0;
} | #include<bits/stdc++.h>
typedef long long ll;
using namespace std;
void prince()
{
#ifndef ONLINE_JUDGE
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
int main() {
prince();
ll n;
cin>>n;
ll sum = 0;
while(n--)
{
ll a,b;
cin>>a>>b;
a--;
sum+=b*(b+1)/2-a*(a+1)/2;
}
cout<<sum<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
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;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<ll> A(n);
vector<ll> sum(n+1);
vector<ll> mx(n+1);
rep(i, n) {
cin >> A[i];
sum[i+1] = sum[i] + A[i];
mx[i+1] = max(sum[i+1], mx[i]);
}
ll tmp = 0;
ll ans = 0;
rep(i, n) {
chmax(ans, tmp+mx[i+1]);
tmp += sum[i+1];
}
cout << ans << endk;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
long long sum=0,sum2=0,maxx=0;
for(int i=1;i<=n;i++)
{
long long x;
cin >> x;
sum+=x;
sum2+=sum;
maxx=max(maxx,x);
cout << sum2+maxx*i << '\n';
}
}
|
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string x;
long long m;
cin >> x >> m;
long long d = 0;
for (char c : x) d = max<long long>(d, c - '0');
if (x.size() == 1) {
if (x[0] - '0' <= m)cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
long long lb = d, ub = m + 1;
while (ub - lb > 1) {
long long mid = (lb + ub) / 2;
long long v = 0;
for (char c : x) {
if (v > (m + mid - 1) / mid) {
v = -1;
break;
}
v *= mid;
v += c - '0';
}
if (v > m) {
v = -1;
}
if (v == -1) ub = mid;
else lb = mid;
}
cout << lb - d << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <string>
#define rep(i,n) for(int i=0;i < (n);i++)
#define rep2(i, s, n) for (int i = (s); 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 ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef unsigned long long ull;
const ll MOD=1000000007ll;
const int MAX=5100000;
using namespace std;
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
ll fact[MAX], fact_inv[MAX];
ll power(ll a, ll b){
ll res=1;
while(b>0){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
// nCr
ll comb(ll n, ll r){
ll t=1000000;
fact[0]=1;
for(int i=0;i<t;i++){ fact[i+1]=fact[i]*(i+1)%MOD;}
fact_inv[t]=power(fact[t], MOD-2);
for(int i=0;i<t;i++){ fact_inv[i]=fact_inv[i+1]*(i+1)%MOD;}
return (fact[n]*fact_inv[r])%MOD*fact_inv[n-r]%MOD;
}
const int N=100;
char x[N];
long long int m,sans,len;
bool check(long long int i){
long long int ans=0;
for(int o=0;o<=len-1;o++){
if(ans>m*1.0/i){break;}
ans=ans*i+x[o]-'0';
if(ans>m){break;}
else if(o==len-1)return 1;
}
return 0;
}
int main(){
cin>>x;
len=strlen(x);
scanf("%lld",&m);
if(len==1){
if(x[0]-'0'<=m)
printf("1\n");
else
printf("0\n");
return 0;
}
int maxn=1;
for(int i=len-1;i>=0;i--){
maxn=max(maxn,x[i]-'0'+1);
}
long long int l=maxn,r=m,mid,best=maxn-1;
while(l<=r){
mid=(l+r)>>1;
if(check(mid)){
best=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
while(check(best+1))best=best+1;
printf("%lld\n",best-maxn+1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pi = pair<int, int>;
using pii = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<ll>;
using vip = vector<pair<int, int>>;
const int MAX_N = 1e5 + 1;
const ll mod = 1e9 + 7;
const ll INF = 1e9;
#define PI 3.141592653589793238
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define mp make_pair
#define loop(i,n,x) for(int i=n;i<x;i++)
#define sz(a) ((int) (a).size())
#define For(n,x) for(int i=1;i<=x;i++)
#define ite iterator it
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t; while(t--){solve();}
void oj()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
bool isPowerOfTwo(ll n)
{
if (n == 0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
void solve()
{
int a, b;
cin >> a >> b;
a += b;
if (a >= 15 && b >= 8)
cout << "1" << endl;
else if ((a >= 10 ) && (b >= 3))
cout << "2" << endl;
else if ((a >= 3))
cout << "3" << endl;
else
cout << "4" << endl;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
oj();
//test;
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define d(x) cout<<x<<" "
#define nl cout<<endl
#define rep(var,init_val,final_val) for(int var = init_val; var < final_val; ++var)
#define tt ll t;cin>>t;while(t--)
#define show(a,b) rep(i,0,b) d(a[i]);nl;
#define mod 1000000007
#define vi vector<int>
#define pii pair<int,int>
const int d4i[] = {-1,0,1,0};
const int d4j[] = {0,1,0,-1};
const int d8i[] = {-1,-1,-1,0,1,1,1,0};
const int d8j[] = {-1,0,1,1,1,0,-1,-1};
// #define ONLINE_JUDGE true
void solve(){
int m , h;
cin>>m>>h;
if(h%m) cout<<"No";
else cout<<"Yes";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 9;
const int N2 = 10000 + 9;
char s[N];
int a[N], b[N2][N];
bool check(int k, int n) {
for (int i = 0; i <= n; ++i) {
int p = a[i] / k, q = a[i] % k;
for (int j = 1; j <= q; ++j) {
b[j][i] = p + 1;
}
for (int j = q + 1; j <= k; ++j) {
b[j][i] = p;
}
}
bool f = true;
for (int j = 1; j <= k; ++j) {
for (int i = 1; i <= n; ++i) {
if (s[i] == '<') {
if (!(b[j][i - 1] < b[j][i])) f = false;
}
else {
if (!(b[j][i - 1] > b[j][i])) f = false;
}
}
}
return f;
}
int main() {
int n; scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 0; i <= n; ++i) {
scanf("%d", a + i);
}
int L = 1, R = N2 - 9;
while (L < R) {
int M = (L + R) >> 1;
if (check(M + 1, n)) L = M + 1;
else R = M;
}
check(L, n);
printf("%d\n", L);
for (int j = 1; j <= L; ++j) {
for (int i = 0; i <= n; ++i) {
printf("%d%c", b[j][i], i < n ? ' ' : '\n');
}
}
return 0;
} | /*
* * * * * * * * * * * * * * * * * *
*
* @author: Xingjian Bai
* @date: 2021-04-10 13:03:21
* @description:
*
*
* @notes:
* g++ -fsanitize=address -ftrapv
* * * * * * * * * * * * * * * * * */
#include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define TIME (double)clock()/CLOCKS_PER_SEC
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int mod = 1000000007;
const ll oo = 1e18;
const ld eps = 1e-8;
#define debug(x) cerr << "(debug mod) " << #x << " = " << x << endl
int n;
string s;
int a[110];
int b[10010][110];
int main() {
ios::sync_with_stdio(false);
cin >> n >> s;
int mn = 1e9;
for (int i = 0; i <= n; i ++) {
cin >> a[i];
if (i)
mn = min (mn, abs(a[i] - a[i - 1]));
}
for (int i = 0; i <= n; i ++) {
for (int j = 1; j <= mn; j ++)
b[j][i] = a[i] / mn;
for (int j = 1; j <= a[i] - a[i] / mn * mn; j ++)
b[j][i] ++;
}
cout << mn << endl;
for (int i = 1; i <= mn; i ++) {
for (int j = 0; j <= n; j ++)
cout << b[i][j] << ' ';
cout << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false
//累乗(繰り返し二乗法)
long long power(long long b,long long e){
long long r=1;
while(e){
if(e&1){
r=(r*b)%MOD;
}
b=(b*b)%MOD;
e >>=1;
}
return r;
}
int main(void){
ll H,W,K;
vector<pair<pair<ll,ll>,char>> HWC;
cin >> H >> W >> K;
REP(i,K){
ll h,w;
char c;
cin >> h >> w >> c;
h--;w--;
HWC.push_back({{h,w},c});
}
const ll MEM = 5100;
ll S[MEM][MEM]={};
REP(i,HWC.size()){
if(HWC[i].second=='D')S[HWC[i].first.first][HWC[i].first.second]=1;
if(HWC[i].second=='R')S[HWC[i].first.first][HWC[i].first.second]=2;
if(HWC[i].second=='X')S[HWC[i].first.first][HWC[i].first.second]=3;
}
ll inv3 = power(3,MOD-2);
ll tinv3 = 2*inv3;
ll dp[MEM][MEM]={};
dp[0][0]=1;
REP(i,H){
REP(j,W){
ll d=0,r=0;
if(i!=0 && S[i-1][j]!=2){
d=dp[i-1][j];
}
if(i!=0 && S[i-1][j]==0){
d=(d*tinv3)%MOD;
}
if(j!=0 && S[i][j-1]!=1){
r=dp[i][j-1];
}
if(j!=0 && S[i][j-1]==0){
r=(r*tinv3)%MOD;
}
dp[i][j]+=d+r;
dp[i][j]%=MOD;
}
//REP(j,W){cout << (dp[i][j]*power(3,H*W-K))%MOD << " ";}cout << endl;
}
cout << (dp[H-1][W-1]*power(3,H*W-K))%MOD << endl;
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl
#define maxn 3e5+50
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long ,long long>pll;
ll gcd(ll a, ll b) {return (!b ? a : gcd(b, a % b));}
ll lcm(ll a, ll b) {return ((a*b)/gcd(a,b));}
const ll mod = 998244353;
const int INF32 = 1<<30;
const ll INF64 = 1LL<<60;
const ld pi = 3.141592653589793;
long long modpow(long long n, long long k,long long mod)
{
if (k == 0) return 1;
long long r = modpow(n * n % mod, k >> 1, mod);
if (k & 1) r = r * n % mod;
return r;
}
void solve(){
int h,w;cin >> h >> w;
vector<vector<char>>s(h,vector<char>(w,'?'));
ll dp[h+1][w+1];
memset(dp,0,sizeof(dp));
int k;cin >> k;
for(int i = 0;i<k;i++){
int x,y;cin >> x >> y;
char c;cin >> c;
x--;y--;
s[x][y] = c;
}
dp[0][0] = 1;
ll inv3 = modpow(3,mod-2,mod);
for(int i = 0;i<h;i++){
for(int j = 0;j<w;j++){
char c = s[i][j];
if(c=='R'||c=='X') dp[i][j+1] = (dp[i][j+1]+dp[i][j])%mod;
if(c=='D'||c=='X') dp[i+1][j] = (dp[i+1][j]+dp[i][j])%mod;
if(c=='?'){
dp[i][j+1] = (dp[i][j+1]+dp[i][j]*2*inv3%mod)%mod;
dp[i+1][j] = (dp[i+1][j]+dp[i][j]*2*inv3%mod)%mod;
}
}
}
cout << dp[h-1][w-1]*modpow(3,h*w-k,mod)%mod;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define reps(i,s,n) for(int i=s; i<n; i++)
#define per(i,n) for(int i=n-1; i>=0; i--)
#define pers(i,n,s) for(int i=n-1; i>=s; i--)
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define lb(v,n) lower_bound(all(v),n)
#define lbi(v,n) lower_bound(all(v),n) - v.begin()
#define ub(v,n) upper_bound(all(v),n)
#define ubi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define IN(n,m,v) rep(i,n) rep(j,m){cin >> v[i][j];}
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
template <typename T>
class BIT {
ve<T> node;
int n;
T find(int m){
T s = 0;
while (m > 0) {
s += node[m];
m -= (m & (-m));
}
return s;
}
public:
BIT(int _n) : n(_n){
node.assign(n+1,0LL);
}
T sum(int a, int b){
T s_a = find(a);
T s_b = find(b);
return s_b - s_a;
}
void update(int m, T x){
m++;
while (m <= n) {
node[m] += x;
m += (m & (-m));
}
}
};
void solve(){
int H,W,M;
cin >> H >> W >> M;
vl A(H,W),B(W,H);
rep(i,M){
ll h,w;
cin >> h >> w;
h--,w--;
chmin(A[h],w);
chmin(B[w],h);
}
ll ans = 1;
reps(i,1,H) {
if(A[i] == 0) break;
ans += A[i];
}
reps(i,1,W){
if(B[i] == 0) break;
ans += B[i];
}
ve<pll> C;
reps(i,1,H){
if(A[i] == 0) break;
C.pb({A[i],i});
}
sort(all(C));
ll s = 1;
BIT<ll> bi(H+10);
rep(i,si(C)){
pll p = C[i];
if(p.fi == 0) continue;
while (s < p.fi) {
if(B[s] == 0) break;
bi.update(B[s],1LL);
s++;
}
//cout << p.fi csp p.se csp bi.sum(p.se,H+10) << endl;
ans -= bi.sum(p.se,H+2);
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
int h,w,m;
int mh=9999999,ml=9999999;
int blockx[200001],blocky[200001];
int xw[200001],yw[200001];
int a[1000001];
struct node
{
int val,pos;
}ww[200001];
void add(int x,int v)
{
while(x<=200000)
{
a[x]+=v;
x+=x&-x;
}
}
int find(int x)
{
int answw=0;
while(x)
{
answw+=a[x];
x-=x&-x;
}
return answw;
}
bool cmp(node x,node y)
{
return x.val<y.val;
}
signed main()
{
cin>>h>>w>>m;
ml=w;mh=h;
for(int i=1;i<=m;i++)
{
cin>>blockx[i]>>blocky[i];
if(blockx[i]==1)
{
ml=min(ml,blocky[i]-1);
}
if(blocky[i]==1)
{
mh=min(mh,blockx[i]-1);
}
}
//cout<<mh<<' '<<ml<<endl;
for(int i=1;i<=ml;i++)
{
yw[i]=h+1;
}
for(int i=1;i<=mh;i++)
{
xw[i]=w+1;
}
for(int i=1;i<=m;i++)
{
yw[blocky[i]]=min(yw[blocky[i]],blockx[i]);
xw[blockx[i]]=min(xw[blockx[i]],blocky[i]);
}
int ans=0;
for(int i=1;i<=mh;i++)ans+=xw[i]-1;
for(int i=1;i<=ml;i++)
{
//cout<<yw[i]<<' ';
ans+=yw[i]-1;
ww[i].pos=i,ww[i].val=yw[i];
add(i,1);
}
int now=1;
sort(ww+1,ww+1+ml,cmp);
//cout<<ans<<endl;
for(int i=1;i<=mh;i++)
{
while(now<=ml&&ww[now].val+1==i)add(ww[now].pos,-1),now++;
ans-=find(xw[i]-1);
}
cout<<ans;
return 0;
}
/*
3 4 3
1 3
3 3
2 2
*/ |
#include<bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef __int128 ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
std::ostream&
operator<<( std::ostream& dest, __int128_t value )
{
std::ostream::sentry s( dest );
if ( s ) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[ 128 ];
char* d = std::end( buffer );
do
{
-- d;
*d = "0123456789"[ tmp % 10 ];
tmp /= 10;
} while ( tmp != 0 );
if ( value < 0 ) {
-- d;
*d = '-';
}
int len = std::end( buffer ) - d;
if ( dest.rdbuf()->sputn( d, len ) != len ) {
dest.setstate( std::ios_base::badbit );
}
}
return dest;
}
ll f(string s,ll x,ll m){
ll sum=0,p=1;
for(ll i=0;i<sz(s);i++){
ll val=(s[i]-'0');
sum+=val*p;
if(sum>m)
return 0;
p*=x;
if(i<sz(s)-1&&p>m)
return 0;
}
return 1;
}
void solve(){
string s;
long long int tr;
cin>>s>>tr;
ll m=tr;
ll d=0;
for(ll i=0;i<sz(s);i++)
d=max(d,ll(s[i]-'0'));
reverse(s.begin(),s.end());
ll l0=d+1,r0=1000000000000000000;
while(l0<=r0){
ll m0=(l0+r0)/2;
if(f(s,m0,m))
l0=m0+1;
else
r0=m0-1;
}
ll res=max(ll(0),r0-d);
if(sz(s)==1){
res=min(res,ll(1));
}
cout<<(long long int)(res);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
clock_t z=clock();
ll qc=1;
//cin>>qc;
for(ll i=1;i<=qc;i++){
solve();
}
debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC);
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
int main(){
string x;
ll m;
cin >> x;
reverse(x.begin(), x.end());
cin >> m;
int basemin = 0;
for(auto &p: x) basemin = max(basemin, p-'0');
basemin++;
if(x.length() == 1){
if(x[0]-'0' <= m) cout << 1 << endl;
else cout << 0 << endl;
}else{
ll ans = 0;
ll ac = 2;
ll wa = 1'000'000'000'000'000'000LL + 1LL;
while(wa-ac > 1){
ll mid = (wa+ac)/2;
ll sum = 0;
ll base = 1;
if(mid < basemin){
ac = mid;
continue;
}
bool iswa = false;
sum += x[0]-'0';
if(sum > m) iswa = true;
for(int i = 1; i < x.length(); i++){
if(base > m/mid){
iswa = true;
break;
}
base *= mid;
//sum += base*(x[i]-'0');
if((x[i]-'0') <= (m-sum)/base){
sum += base*(x[i]-'0');
}else{
iswa = true;
break;
}
}
if(iswa) wa = (ll)mid;
else ac = (ll)mid;
}
cout << max(0LL, ac-basemin+1) << endl;
}
return 0;
} |
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
constexpr long long INFLL = 1e18;
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll x, y, a, b;
cin >> x >> y >> a >> b;
bool check = false;
ll exp = 0;
while (x < b) {
if (x >= y) {
x /= a;
exp--;
check = true;
break;
}
x *= a;
exp++;
}
if (check == false && x >= y) {
x /= a;
exp--;
}
if (check == false && y > x) {
ll dif = y - x;
ll count = dif / b;
if (dif % b == 0 && count > 0) {
count--;
}
exp += count;
// x += b * count;
/*
while (x < y) {
x += b;
exp++;
}
x -= b;
exp--;
*/
}
// cout << x << endl;
if (exp < 0) {
exp = 0;
}
cout << exp << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9+7;
int mod_pow(int a,int b,int M = MOD)
{
if(a == 0) return 0;
b %= (M - 1); //M must be prime here
int res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
void solve()
{
int n,m,q;
cin >> n >> m >> q;
pii a[n];
f(i,n) cin >> a[i].S >> a[i].F;
sort(a,a+n);
reverse(a,a+n);
int b[m];
f(i,m) cin >> b[i];
int l,r;
while(q--)
{
cin >> l >> r;
l--,r--;
multiset<int> ms;
for(int i=0;i<m;i++)
if(i < l || i > r) ms.insert(b[i]);
int res = 0;
f(i,n)
{
if(ms.empty()) break;
auto it = ms.lower_bound(a[i].S);
if(it == ms.end()) continue;
ms.erase(it);
res += a[i].F;
}
cout << res << '\n';
}
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int a[N],b[N];
int c[N],d[N];
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
for(int i=1;i<=n;++i)
cin>>b[i];
int sum=0,ans=0,m=(n>>1);
for(int i=1;i<=n;++i){
sum+=a[i];
b[i]-=a[i];
if(i&1) c[(i+1)/2]=b[i];
else d[i/2]=b[i];
}
sort(c+1,c+m+1,greater<int>());
sort(d+1,d+m+1,greater<int>());
ans=sum;
for(int i=1;i<=m;++i){
sum+=c[i]+d[i];
ans=max(ans,sum);
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const int MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;}
int main() {
int n;
scanf("%d", &n);
map<int, int> ma;
ll ans = 0;
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
t %= 200;
ans += ma[t]++;
}
printf("%lld", ans);
}
// author: rdd6584
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
int solve()
{
int h, w, di[4] = {0, 1, 0, -1}, dj[4] = {1, 0, -1, 0};
char c;
cin >> h >> w;
vector<vector<pair<int, int>>> alphabet(26);
vector<vector<char>> grid(h, vector<char>(w));
vector<vector<bool>> vis(h, vector<bool>(w));
pair<int, int> cur = {0, 0};
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> grid[i][j];
if (grid[i][j] == 'S')
{
cur.first = i;
cur.second = j;
}
else if (grid[i][j] >= 'a' && grid[i][j] <= 'z')
{
alphabet[grid[i][j] - 'a'].pb({i, j});
}
}
}
int time = 1;
queue<pair<int, int>> q;
q.push(cur);
vis[cur.first][cur.second] = true;
while (!q.empty())
{
int len = q.size();
while (len-- > 0)
{
pair<int, int> from = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++)
{
int i = from.first + di[dir], j = from.second + dj[dir];
if (i >= 0 && i < h && j >= 0 && j < w && grid[i][j] != '#' && !vis[i][j])
{
if (grid[i][j] == 'G')
return time;
vis[i][j] = true;
q.push({i, j});
}
}
if (grid[from.first][from.second] >= 'a' && grid[from.first][from.second] <= 'z')
{
for (pair<int, int> tel : alphabet[grid[from.first][from.second] - 'a'])
{
if (grid[tel.first][tel.second] == 'G')
return time;
if (vis[tel.first][tel.second])
continue;
vis[tel.first][tel.second] = true;
q.push(tel);
}
}
}
time++;
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << solve() << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = s; i < (int)(n); ++i)
#define Clear(a) a = decltype(a)()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vec vector
typedef long long ll;
typedef pair<ll,ll> P;
typedef string::const_iterator state; //構文解析
const double PI=acos(-1);
//const ll big=998244353;
const ll big=1000000007LL;
const ll INF=1e18;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
if(x<y)return x;
else return y;
}
ll expm(ll x,ll y,ll mod=big){
if(y==0)return 1;//0^0=1
if(x==1||x==0)return x;
if(y%2==1)return (expm(x,y-1,mod)*x)%mod;
ll t=expm(x,y/2,mod);
return (t*t)%mod;
}
ll exp(ll x,ll y){
if(y==0)return 1;//0^0=1
if(x==1||y==0)return x;
if(y%2==1)return exp(x,y-1)*x;
ll t=exp(x,y/2);
return t*t;
}
struct edge{
ll to,cost;
};
struct graph{
vector<vector<edge>> g;
vector<ll> d;
graph(ll n) : g(n),d(n,INF){}
void addedge(const ll s,const ll t,const ll cost){
edge e;
e.to=t;
e.cost=cost;
g[s].emplace_back(e);
}
//E+VlogV
void dijkstra(ll s){//sからstartのdijkstra
d[s]=0;
queue<P> q1,q2;
q1.push(P(0,s));
while(!q1.empty() || !q2.empty()){
if(!q1.empty()){
P pa=q1.front();q1.pop();
ll k=pa.second;
if(d[k]<pa.first)continue;
for(auto e:g[k]){
if(d[e.to]>d[k]+e.cost){
d[e.to]=d[k]+e.cost;
if(e.cost==1)q1.push(P(d[e.to],e.to));
else q2.push(P(d[e.to],e.to));
}
}
}
else{
P pa=q2.front();q2.pop();
ll k=pa.second;
if(d[k]<pa.first)continue;
for(auto e:g[k]){
if(d[e.to]>d[k]+e.cost){
d[e.to]=d[k]+e.cost;
if(e.cost==1)q1.push(P(d[e.to],e.to));
else q2.push(P(d[e.to],e.to));
}
}
}
}
}
};
int h,w;
int point(int i,int j){
return i*w+j;
}
pair<int,int> point_back(int k){
return {k/w , k%w};
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout.precision(10);
/*--------------------------------*/
cin >> h >> w;
vector<vector<char>> g(h,vector<char>(w));
map<char,vector<int>> ma;
int s,G;
rep(i,h)rep(j,w){
cin >> g[i][j];
if('a'<=g[i][j] && g[i][j] <= 'z'){
ma[g[i][j]].emplace_back(point(i,j));
}
if(g[i][j]=='S'){
s=point(i,j);
}if(g[i][j]=='G'){
G=point(i,j);
}
}
graph gra(h*w+26);
//全部コストを2倍で計算.
for(char c='a';c<='z';c++){
rep(i,ma[c].size()){
gra.addedge(ma[c][i] , c-'a' + h*w , 1);
gra.addedge(c-'a' + h*w , ma[c][i],1);
}
}
rep(i,h)rep(j,w){
rep(k,4){
int ni=i+dx[k];
int nj=j+dy[k];
if(ni<0 || ni>=h || nj<0 || nj>=w || g[ni][nj]=='#')continue;
gra.addedge(point(i,j),point(ni,nj),2);
}
}
gra.dijkstra(s);
if(gra.d[G]>=INF)cout << -1 << endl;
else cout << gra.d[G]/2 << endl;
} |
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
#define int lint
const int INF = __LONG_LONG_MAX__;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i
signed main()
{
int n,m;
cin>>n>>m;
vector<int> H(n);
vector<int> W(m);
for(auto &h:H) cin>>h;
for(auto &w:W) cin>>w;
sort(H.begin(),H.end());
vector<int> sum(n+1,0);
for(int i=0;i<n;i++){
if(i&1)
sum[i+1]=sum[i]+H[i];
else
sum[i+1]=sum[i]-H[i];
}
int res=INF;
for(auto w:W){
int index=lower_bound(H.begin(),H.end(),w)-H.begin();
if(index&1)
index--;
int mid=abs(H[index]-w);
int top=(-1)*(sum[n]-sum[index+1]);
int bottom=sum[index];
res=min(res,top+mid+bottom);
}
cout<<res<<endl;
return 0;
} | #include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define umap unordered_map
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MN(a,b,c) min(a,min(b,c))
#define MX(a,b,c) max(a,max(b,c))
#define pr1 pair<ll,ll>
#define F first
#define S second
#define mP make_pair
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,x,y) for(ll i=x;i<=y;i++)
#define f2(i,x,y) for(ll i=x;i>=y;i--)
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define modsum(a,b) ((a%mod)+(b%mod))%mod
#define modpro(a,b) ((a%mod)*(b%mod))%mod
#define moddif(a,b) ((a%mod)-(b%mod)+mod)%mod
//__builtin_popcount(x)
//__builtin_parity(x) =(number of set bits)%2
//__builtin_clz(x) to count the number of leading zeroes
//__builtin_ctz(x) to count the number of trailing zeroes
//__gcd(a,b)
using namespace std;
ll Fact(ll x)
{
ll y=1;
for(ll i=1;i<=x;i++){y=modpro(y,i);}
return y;
}
ll modularExponentiation(ll x,ll n,ll M)
{
ll result=1;
while(n>0)
{
if(n % 2 ==1)
result=((result%M) * (x%M))%M;
x=((x%M)*(x%M))%M;
n=n/2;
}
return result;
}
ll binaryExponentiation(ll x,ll n)
{
ll result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
ll pow1(ll x,ll y)
{
ll z=1;
while(y--){z=z*x;}
return z;
}
bool isprime(ll n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
ll check(ll x)
{
ll y=1,z=0;
while(x>y){y*=2;z++;}
if(x!=y){z--;}
return z;
}
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p)
{
return modularExponentiation(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n,
int r, int p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
unsigned long long fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
ll modInverse(ll A,ll M)
{
return modularExponentiation(A,M-2,M);
}
bool cmp(pair<ll,ll> a, pair<ll,ll> b)
{
if(a.F==b.F)
return a.S<b.S;
else
return a.F<b.F;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
vector <ll> a(n),b(m);
f(i,n) cin>>a[i];
f(i,m) cin>>b[i];
sort(all(a));
sort(all(b));
ll mn=1e18;
vector <ll> start(n),end(n);
f(i,n)
{
if(i%2==1) start[i]=start[i-1]+abs(a[i]-a[i-1]);
else
{
if(i==0) start[i]=0;
else start[i]=start[i-1];
}
}
for(ll i=n-1;i>=0;i--)
{
ll j=n-1-i;
if(j%2==1) end[i]=end[i+1]+abs(a[i]-a[i+1]);
else
{
if(j==0) end[i]=0;
else end[i]=end[i+1];
}
}
f(i,m)
{
ll y=(upper_bound(a.begin(),a.end(),b[i])-a.begin());
//cout<<y<<"\n";
if(y%2==1)
{
ll val=abs(b[i]-a[y-1]);
if((y-2)>=0) val+=start[y-2];
if(y<n) val+=end[y];
//cout<<i<<' '<<val<<"\n";
mn=min(mn,val);
}
else
{
ll val=abs(b[i]-a[y]);
if((y-1)>=0) val+=start[y-1];
if((y+1)<n) val+=end[y+1];
//cout<<i<<' '<<val<<"\n";
mn=min(mn,val);
}
}
cout<<mn<<"\n";
}
|
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef pair<int,int> Pin;
typedef long long ll;
ll INF = 3e18;
int inf = 1e9;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
template <typename T>
void print(vector<T>& x) {
int N = x.size();
REP(i, N) {
if (i > 0) cout << " ";
cout << x[i];
}
cout << "\n";
}
/// unrolled extgcd.
/// Solve ax + by = gcd(a, b) and return gcd(a, b).
/// x and y are returned as given args.
/// Note that a^-1 mod n can be calculated by x of extgcd(a, n, x, y)
ll extgcd(ll a, ll b, ll &x, ll &y) {
ll u[] = {a, 1, 0}; ll v[] = {b, 0, 1};
while(*v) {
ll t = *u / *v;
REP(i, 3) swap(u[i]-=t * v[i], v[i]);
}
x = u[1]; y = u[2];
return u[0]; // return gcd of a and b
}
ll mod(ll a, ll m) {
return (a % m + m) % m;
}
/// solve x ≡ t_i mod m_i.
/// return {ans, lcm} if there is an answer.
/// Otherwise, return {0, -1}.
pair<ll, ll> crt(const vector<ll>& r, const vector<ll>& m) {
assert(r.size() == m.size());
ll r0 = 0, m0 = 1; // rem == ans, lcm
REP(i, (int) r.size()) {
assert(1 <= m[i]);
ll r1 = mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) swap(m0, m1), swap(r0, r1);
if (m0 % m1 == 0) {
if (r0 % m1 != r1) return {0, -1};
continue;
}
ll x, y;
ll g = extgcd(m0, m1, x, y);
if ((r1 - r0) % g) return {0, -1};
ll u1 = m1 / g;
ll tmp = (r1 - r0) / g % u1 * x % u1;
r0 += m0 * tmp;
m0 *= u1;
if (r0 < 0) r0 += m0;
}
return {mod(r0, m0), m0}; // => {rem, lcm}
}
void solve() {
ll x, y, p, q;
cin >> x >> y >> p >> q;
ll ans = -1;
FOR(t1, p, p + q) {
FOR(t2, x, x + y) {
auto [t, lcm] = crt({t1, t2}, {p + q, 2 * (x + y)});
if (lcm == -1) continue;
if (ans == -1) ans = t;
else ans = min(ans, t);
}
}
if (ans == -1) print("infinity");
else print(ans);
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int t; cin >> t;
while(t--) solve();
}
| #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define ceil(x, y) (((x)+(y)-1) / (y))
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define inf 1e18
using namespace std;
typedef long long llint;
typedef long long ll;
typedef pair<ll, ll> P;
bool exceed(ll x, ll y, ll m){return x >= m / y + 1;}
struct edge{
ll to, cost;
edge(){}
edge(ll a, ll b){
to = a, cost = b;
}
};
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
for(int i = 0; i<vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
}
return os;
}
template<typename T, typename U>
ostream& operator << (ostream& os, pair<T, U>& pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
template<typename T, typename U>
ostream& operator << (ostream& os, map<T, U>& map_var) {
for(typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if(itr != map_var.end()) os << ",";
itr--;
}
return os;
}
template<typename T>
ostream& operator << (ostream& os, set<T>& set_var) {
for(typename set<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if(itr != set_var.end()) os << " ";
itr--;
}
return os;
}
void dump_func() {cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
cout << head;
if(sizeof...(Tail) > 0) cout << " ";
dump_func(std::move(tail)...);
}
ll n;
string s, x;
ll dp[200005][7];
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> x;
s = "#" + s, x = "#" + x;
dp[n][0] = 1;
rep(i, 1, 6) dp[n][i] = -1;
for(int i = n-1; i >= 0; i--){
rep(j, 0, 6){
if(x[i+1] == 'T') dp[i][j] = max(dp[i+1][j*10%7], dp[i+1][(j*10+s[i+1]-'0')%7]);
if(x[i+1] == 'A') dp[i][j] = min(dp[i+1][j*10%7], dp[i+1][(j*10+s[i+1]-'0')%7]);
}
}
if(dp[0][0] > 0) outl("Takahashi");
if(dp[0][0] < 0) outl("Aoki");
return 0;
} |
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define _roverload(_a1, _a2, _a3, 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(...) _roverload(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i)
#define rrep(...) _roverload(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void print() { cout << '\n'; }
template <class T>
void print(const vector<T>& v) {
bool f = false;
for (const auto& e : v) {
if (f) cout << ' ';
cout << e;
f = true;
}
cout << '\n';
}
template <class... T>
void print(const T&... args) {
bool f = false;
for (const auto& e : {args...}) {
if (f) cout << ' ';
cout << e;
f = true;
}
cout << '\n';
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
const int MOD = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime MOD
mint inv() const {
return pow(MOD - 2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) const {
return x == a.x;
}
bool operator!=(const mint a) const {
return x != a.x;
}
friend ostream& operator<<(ostream& os, const mint& value) {
os << value.x;
return os;
}
friend istream& operator>>(istream& is, mint& value) {
ll t;
is >> t;
value = mint(t);
return is;
}
};
#pragma endregion library
int main() {
int n, m;
cin >> n >> m;
mint mm = m;
mint ans = mm.pow(n) * n;
vector<mint> mpow(n + 1);
mpow[0] = 1;
rep(i, n) {
mpow[i + 1] = mpow[i] * m;
}
rep(v, 1, m + 1) {
vll vs(n);
vs[0] = 1;
mint now = 1;
mint mv = 1;
rep(i, 1, n) {
ans -= now * mpow[n - i - 1];
now *= m;
mv *= m - v;
now += mv;
}
}
print(ans);
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
int a,b;
a=(x+y)/2;
b=(x-y)/2;
cout<<a<<" "<<b<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define y1 orzkcz
using namespace std;
const int N=500005;
int n,L,a[N],b[N],p[N];
int main(){
scanf("%d%d",&n,&L);
For(i,1,n) scanf("%d",&a[i]);
For(i,1,n) scanf("%d",&b[i]);
a[n+1]=b[n+1]=L+1;
For(i,1,n+1) a[i]-=i,b[i]-=i;
For(i,1,n+1){
p[i]=lower_bound(a,a+n+2,b[i])-a;
if (a[p[i]]!=b[i]) return puts("-1"),0;
//cout<<p[i]<<' '; cout<<endl;
}
ll ans=0;
For(i,1,n+1) if (b[i]!=b[i-1]){
int pp=p[i-1],v=max(i-1-pp,0);
for (;a[pp+1]==a[pp];++pp) v=min(v,max(0,i-1-(pp+1)));
ans+=v+max(p[i]-i,0);
}
cout<<ans<<endl;
} | /*input
10 1000000000
65110170 68805223 123016442 275946481 661490312 760727752 764540566 929355340 930658577 947099792
1 2 123016442 661490311 929355337 930658574 999999997 999999998 999999999 1000000000
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define eb emplace_back
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (int)(lower_bound(c.begin(),c.end(),x)-c.begin())
#define EL cout<<'\n'
#define BS(a,x) binary_search(ALL(a),x)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<'('<<P.F<<','<<P.S<<')';
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,SZ(V)) out<<V[i]<<((i!=SZ(V)-1)?" ":"");
return out;
}
//}}}
const ll maxn=300005;
const ll maxlg=20;
const ll INF64=1e18;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;
ll mypow(ll a,ll b){
ll res=1LL;
while(b){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
ll a[maxn],b[maxn];
vector<ll> A, B;
ll ans = 0;
int main(){
IOS;
int n;
ll L;
cin>>n>>L;
REP(i,n) cin>>a[i+1];
REP(i,n) cin>>b[i+1];
a[n+1] = b[n+1] = L+1;
for(int i=0;i<n+1;i++){
A.pb(a[i+1]-a[i]-1);
}
for(int i=0;i<n+1;i++){
B.pb(b[i+1]-b[i]-1);
}
// cout<<A<<'\n';
// cout<<B<<'\n';
int pt = 0;
REP(i,SZ(B)) if(B[i]){
ll left = B[i];
int first_non_zero = -1;
while(pt<SZ(A)&&left>0){
left -= A[pt];
if(A[pt]!=0&& first_non_zero == -1) first_non_zero = pt;
pt++;
}
if(left<0){
cout<<"-1\n";
return 0;
}
if(first_non_zero <= i && i <= pt-1){
ans+=pt-1-first_non_zero;
}
else if(first_non_zero > i){
ans += pt-1-i;
}
else ans += i-first_non_zero;
// cout<<i<<' '<<first_non_zero<<' '<<pt-1<<'\n';
}
cout<<ans<<'\n';
return 0;
}
|
/**
Bismillahir Rahmanir Raheem
Author:Refatul Fahad
Bangabandhu Sheikh Mujibur Rahman Science & Technology University, Gopalganj-8100.
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define grv(v) sort(v.begin(),v.end(),greater<int>())
#define sz(s) (int)s.size()
#define all(s) s.begin(),s.end()
#define fill(a,b) memset(a,b,sizeof(a))
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define countv(v,a) count(v.begin(),v.end(),a)
#define el "\n"
#define IOS ios_base::sync_with_stdio(false),cin.tie(NULL)
#define pr(x) cerr << #x << " ** " << x << endl;
const int N = 1e5 + 5;
const ll M = (ll)1e9 + 7;
const ll inf=(ll)1e18;
const double pi=2*acos(0.0);
const double esp=1e-9;
int dx[]= {0,0,1,-1};
int dy[]= {1,-1,0,0};
int main() {
//IOS;
/// freopen("input.txt","r",stdin);
/// freopen("output.txt","w",stdout);
int test=1;
//cin>>test;
for(int cs=1; cs<=test; ++cs) {
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<el;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> A(3);
for(int i=0;i<3;i++) cin >> A.at(i);
sort(A.begin(),A.end());
if(A.at(2) - A.at(1) == A.at(1) - A.at(0)){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
//cout << A.at(1) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
const lint MOD =1000000007;
//const lint MOD=998244353;
const lint MAX = 4000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#pragma GCC optimize("Ofast")
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
lint fac[MAX], finv[MAX], inv[MAX];
void COMinit()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (lint 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(lint n, lint k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
lint primary(lint num)
{
if (num < 2) return 0;
else if (num == 2) return 1;
else if (num % 2 == 0) return 0;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
lint lcm(lint a,lint b){
return a/__gcd(a,b)*b;
}
lint gcd(lint a,lint b){
return __gcd(a,b);
}
int main(){
lint n,d,h;
cin>>n>>d>>h;
P p[n];
double ans=0;
rep(i,n){
lint a,b;
cin>>a>>b;
long double kata=(double)(h-b)/(d-a);
ans=max(ans, double(h-kata*d));
}
printf("%.12f\n",ans);
}
| ///Bishmillahir Rahmanir Rahim,In the name of Almighty Allah(swt)
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define lop(i,n) for(i=0;i<n;i++)
#define lop1(i,n) for(i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.14159265358979323846
#define smile ios_base::sync_with_stdio(false); cin.tie(NULL);
//void file();
//void error();
int main()
{
smile //file();
int t;
// cin >> t;
// for (int z = 1; z <= t; z++) {
int n, i, j;
int a[2][2];
lop(i, 2) {
lop(j, 2)
cin >> a[i][j];
}
int b,c;
b = a[0][0]*a[1][1];
c = a[0][1]*a[1][0];
cout<<b-c<<endl;
// }
//error();
return 0;
}
/*
void file() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
}
void error()
{
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}
*/ |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
// 与えられた値を記録する
// Xは18桁を超えるのでstring型としておく
// Mは9桁を超え、18桁を超えないのでint64_t型としておく
string X; cin >> X; int64_t M; cin >> M;
// Xが1桁の場合は概算時に面倒なので先に算出してしまう(面倒な理由は後述)
if (X.size() == 1) {
// Xが1桁の場合は得られる値がXの数字そのものとなる('8'の場合8)
// これは、Xが何進数であろうとも変わらない
// そのため、得られる値がM超過であった場合は何進数であろうともMを超える
// 例:X = '8', M = 7 => 得られる値は常に8なのでM以下の場合は存在しない => 0種類
// 得られる値がM以下であった場合、何進数であろうともM以下となる
// なので、n進数のnは無限に存在することになるが、
// 「得られる値のうち、M以下であるようなものは"何種類"」と聞かれているので
// 1種類ということになる
// 例:X = '8', M = 9 => 得られる値は常に8なのでM以下は8の1種類 => 1種類
cout << (stoll(X) <= M) << endl; return 0;
}
// Xに含まれる最も大きい数字dを探索する
int64_t d = 0;
rep(i, X.size()) if (d < stoi(string(1, X.at(i)))) {
d = stoi(string(1, X.at(i)));
}
int64_t ans = (int64_t)pow(M / stoi(string(1, X.at(0))), 1 / (double)(X.size() - 1));
ans = max(d, ans - 1);
while (true) {
int64_t temp = 0;
rep(i, X.size()) {
if (temp != 0 && temp >= LLONG_MAX / ans) goto BREAK;
temp = temp * ans + stoi(string(1, X.at(i)));
}
if (temp > M) goto BREAK;
ans++;
}
BREAK:
ans = max((int64_t)0, ans - d - 1);
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxN = 1e6 + 10;
int n;
string s;
int a[maxN];
vector<int> v[maxN];
unsigned long long m;
int main()
{
cin>>s;
cin>>m;
assert(s[0] > '0');
if (s.size() == 1)
{
if (s[0] -'0' <= m) printf("1\n"); else
printf("0\n");
return 0;
}
unsigned long long lo = 0;
unsigned long long ro = 2e18 + 10;
for (int i : s)
if (lo < i - '0') lo = i - '0';
unsigned long long d = lo;
while(lo < ro - 1)
{
unsigned long long mid = (lo + ro ) / 2;
unsigned long long xi = 0;
int bad = 0;
for (int i : s)
{
if (m/mid < xi) {
bad = 1;
break;
}
xi *= mid;
xi += (i-'0');
}
if (bad) ro = mid; else
if (xi <= m) lo = mid; else ro = mid;
}
cout<<lo - d<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int w=1,x=0;char f=getchar();
while(!isdigit(f) && f!='-') f=getchar();
if(f=='-') {w=-1;f=getchar();}
while(isdigit(f)) {x=(x<<3)+(x<<1)+f-48;f=getchar();}
return w*x;
}
int a,b,ans;
int main()
{
a=read();b=read();
ans=2*a+100-b;
printf("%d\n",ans);
return 0;
} | #include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
ll T = 1, i, j;
//cin >> T;
while (T--) {
ll n;
cin >> n;
map<ll,set<ll>> s;
vector<ll> a(n), b(n);
for(i = 0; i < n; i++){
cin >> a[i];
s[a[i] + i].insert(i);
}
for(i = 0; i < n; i++){
cin >> b[i];
}
indexed_set st;
ll ans = 0;
for(i = 0; i < n; i++){
ll sum = b[i] + i;
if(s[sum].empty()){
cout << -1 << endl;
return 0;
}
ll pos = *s[sum].begin();
ll posi = st.order_of_key(pos);
ans += pos - posi;
s[sum].erase(s[sum].begin());
st.insert(pos);
}
cout << ans << endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.in", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
--a; --b; g[a].push_back(b);
}
int ans = 0;
vector<int> vis(n, 0);
auto dfs = [&](int b, const auto & self) -> int {
int c = 1;
vis[b] = 1;
for (auto i : g[b]) {
if (!vis[i]) {
c += self(i, self);
}
}
return c;
};
for (int i = 0; i < n; i++) {
vis.assign(n, 0);
ans += dfs(i, dfs);
}
cout << ans << "\n";
return 0;
} | #include <iostream>
#include <bitset>
#define REP(i, n) for (int i = 0; i < (n); i++)
std::bitset<2000> d[2000];
int n, m, a, b, res;
int main() {
std::cin >> n >> m;
REP(i, n)
d[i][i] = 1;
REP(i, m) {
std::cin >> a >> b;
d[--a][--b] = 1;
}
REP(i, n) REP(j, n) if (d[j][i])
d[j] |= d[i];
REP(i, n)
res += d[i].count();
std::cout << res << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
typedef tuple<int, int, int, int> T;
vector<T> a( n );
for( int i = 0; i < n; i++ ) {
int x, y, r;
cin >> x >> y >> r;
a[i] = T( x, y, r, i );
}
sort( a.begin(), a.end() );
vector<T> ans( n );
for( int i = 0; i < n; i++ ) {
int x, y, r, j;
tie( x, y, r, j ) = a[i];
if( i == 0 ) {
int mi = INT32_MAX;
int x0 = 0;
int y0 = 0;
for( int k = 0; k <= x; k++ ) {
for( int l = 0; l <= y; l++ ) {
int d = abs((x + 1 - k) * (y + 1 - l) - r);
if( d < mi ) {
mi = d;
x0 = k;
y0 = l;
}
}
}
ans[j] = T( x0, y0, x + 1, y + 1 );
}
else if( i == n - 1 ) {
int mi = INT32_MAX;
int x0 = 0;
int y0 = 0;
for( int k = 10000; k > x; k-- ) {
for( int l = 10000; l > y; l-- ) {
int d = abs((k - x) * (l - y) - r);
if( d < mi ) {
mi = d;
x0 = k;
y0 = l;
}
}
}
ans[j] = T( x, y, x0, y0 );
}
else {
int x1, y1, r1, j1;
tie( x1, y1, r1, j1 ) = a[i + 1];
int x2, y2, r2, j2;
tie( x2, y2, r2, j2 ) = a[i - 1];
if( x == x1 || x == x2 ) {
for( int k = i + 1; k < n; k++ ) {
tie( x2, y2, r2, j2 ) = a[k];
if( x2 > x ) break;
}
int mi = INT32_MAX;
int x0 = -1;
int y0 = -1;
for( int l = y + 1; l <= y1; l++ ) {
for( int k = x + 1; k <= x2; k++ ) {
int d = abs((k - x) * (l - y) - r);
if( d < mi ) {
mi = d;
x0 = k;
y0 = l;
}
}
}
if( y0 < 0 ) {
x0 = x + 1;
y0 = y + 1;
}
ans[j] = T( x, y, x0, y0 );
}
else {
int mi = INT32_MAX;
int x0 = -1;
int ll = 0;
for( int l = 1; l <= 10000; l++ ) {
for( int k = x + 1; k <= x1; k++ ) {
int d = abs((k - x) * l - r);
if( d < mi ) {
mi = d;
ll = l;
x0 = k;
}
}
}
if( x0 < 0 ) {
x0 = x + 1;
ll = 1;
}
int y0 = y;
int y2 = y + ll;
if( y2 > 10000 ) {
y0 = y - (y2 - 10000);
y2 = 10000;
}
ans[j] = T( x, y0, x0, y2 );
}
}
}
for( int i = 0; i < n; i++ ) {
int x, y, x1, y1;
tie( x, y, x1, y1 ) = ans[i];
cout << x << " " << y << " " << x1 << " " << y1 << endl;
}
}
| #include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
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; }
__attribute__ ((constructor))
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
}
int main(){
int n;
cin >> n;
vector<int> a(n), b(n), r(n);
rep(i, n) cin >> a[i] >> b[i] >> r[i];
vector<int> a_, b_, c_, d_;
rep(i,n) {
a_.push_back(a[i]);
b_.push_back(b[i]);
c_.push_back(a[i] + 1);
d_.push_back(b[i] + 1);
}
rep(i, n){
cout << a_[i] << " " << b_[i] << " " << c_[i] << " " << d_[i] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define ll long long
#define maxN 100000 + 5
#define IO ios::sync_with_stdio(0);cin.tie(0)
#define endl '\n'
#define x first
#define y second
#define pii pair<int, int>
#define pll pair<ll int, ll int>
#define sz(s) (int)s.size()
using namespace std;
pll finda(pll a) {
ll int tmp = a.y/2;
if(a.y % 2) {
return {-a.x - tmp, -a.x + tmp};
}
else{
return {a.x - tmp, a.x + tmp - 1};
}
}
signed main()
{
IO;
ll int b, c;
cin >> b >> c;
if(c == 1 && b != 0) {
cout << "2" << endl;
return 0;
}
auto [tmpa, tmpb] = finda({b, c});
auto [tmpc, tmpd] = finda({b, c - 1});
ll int ans = tmpb - tmpa + 1 + tmpd - tmpc + 1 - max(0ll, min(tmpb,tmpd) - max(tmpa, tmpc) + 1);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <std::ostream& os = std::cout>
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
template <typename T>
auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
for (size_t i = 0; i < sz; i++)
os << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
os << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { os << term; }
void print_rest() const { os << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints<> prints;
Prints<std::cerr> prints_err;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
ll N, M;
cin >> N >> M;
ll x = 1;
ll c = 10;
for (; N > 0; N >>= 1) {
if (N & 1) x = x * c % (M * M);
c = c * c % (M * M);
}
prints()(x / M);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
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 <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define X first
#define Y second
#define pb push_back
#define sz(s) int64_t(s.size())
#define make_unique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
const ll mod = 1e9 + 7;
template<class T>
istream& operator >> (istream& in, vector<T>& v){ for (auto &x : v) { in >> x; } return in; }
template<class T, class U>
istream& operator >> (istream& in, pair<T, U> & v){ in >> v.X >> v.Y;return in; }
template<class T, class U>
ostream& operator << (ostream& out, pair<T, U> & v){ out << v.X << " " << v.Y;return out; }
template<class T, class U>
void chkmax(T &a, U b) {
a = max(a, (T)b);
return;
}
template<class T, class U>
void chkmin(T &a, U b) {
a = min(a, (T)b);
return;
}
ll ppow (ll x, ll s) {
if (!s) return 1;
if (!(s - 1)) return x % mod;
if (s % 2) return (x * ppow (x, s - 1)) % mod;
ll b = ppow (x, s / 2);
return (b * b) % mod;
}
vector<int> zf (string s) {
int n = sz(s);
vector<int> z (n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r)
z[i] = min (r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
z[i]++;
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
vector<int> pf(string p) {
int n = sz(p);
vector<int> pref_fun(n, 0);
for (int i = 1;i < n;i++) {
int j = pref_fun[i - 1];
while (j > 0 && p[i] != p[j])
j = pref_fun[j - 1];
if (p[i] == p[j])
j++;
pref_fun[i] = j;
}
return pref_fun;
}
vector<vector<int> > mul(vector<vector<int> > &a, vector<vector<int> > &b) {
int n = sz(a);
vector<vector<int> > c(n, vector<int> (n, 0));
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++) {
for (int k = 0;k < n;k++) {
ll ga = a[i][k];
ga *= b[k][j];
ga %= mod;
c[i][j] += ga;
if (c[i][j] >= mod) {
c[i][j] -= mod;
}
}
}
}
return c;
}
vector<vector<int> > matrix_pow(vector<vector<int> > a, ll y) {
int n = sz(a);
vector<vector<int> > res(n, vector<int> (n, 0));
for (int i = 0;i < n;i++) res[i][i] = 1;
while (y) {
if (y%2 == 1) {
res = mul(res, a);
y--;
}else {
a = mul(a, a);
y /= 2;
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
map<int,int> cnt;
while (n--) {
int x;
cin >> x;
cnt[x]++;
}
vector<int> go;
for (auto x : cnt) go.pb(x.X);
ll ans = 0;
for (auto i : go) {
for (auto j : go) {
ll w = (i - j) * (i - j);
ans += w * cnt[i] * cnt[j];
}
}
cout << ans / 2;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int n;
long long a[N],s[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
s[i]=s[i-1]+a[i];
}
long long ans = 0;
for(int i=1;i<=n;i++) ans += (n - 1) * a[i] * a[i];
for(int i=1;i<=n;i++) ans -= 2 * a[i] * (s[n] - s[i]);
printf("%lld\n",ans);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template <class Abel> struct BIT {
vector<Abel> dat[2];
Abel UNITY_SUM = 0; // to be set
/* [1, n] */
BIT(int n) { init(n); }
void init(int n) { for (int iter = 0; iter < 2; ++iter) dat[iter].assign(n + 1, UNITY_SUM); }
/* a, b are 1-indexed, [a, b) */
inline void sub_add(int p, int a, Abel x) {
for (int i = a; i < (int)dat[p].size(); i += i & -i)
dat[p][i] = dat[p][i] + x;
}
inline void add(int a, int b, Abel x) {
sub_add(0, a, x * -(a - 1)); sub_add(1, a, x); sub_add(0, b, x * (b - 1)); sub_add(1, b, x * (-1));
}
/* a is 1-indexed, [a, b) */
inline Abel sub_sum(int p, int a) {
Abel res = UNITY_SUM;
for (int i = a; i > 0; i -= i & -i) res = res + dat[p][i];
return res;
}
inline Abel sum(int a, int b) {
return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) - sub_sum(1, a - 1) * (a - 1);
}
/* debug */
void print() {
for (int i = 1; i < (int)dat[0].size(); ++i) cout << sum(i, i + 1) << ",";
cout << endl;
}
};
int main(){
ll h,w,m; cin >> h >> w >> m;
vector<P> o(m);
rep(i,m){
cin >> o[i].first >> o[i].second;
o[i].first--; o[i].second--;
}
vector<int> index[h+1];
rep(i,m) index[o[i].first].pb(o[i].second);
rep(i,h) sort(ALL(index[i]));
if(index[0].size() > 0){
int t = index[0][0];
index[0] = {};
FOR(i,t,w) index[0].pb(i);
}
vector<int> tmp;
rep(i,m){
if(o[i].second == 0) tmp.pb(o[i].first);
}
if(tmp.size() > 0){
sort(ALL(tmp));
int u = tmp[0];
FOR(i,u,h){
if(index[i].size() > 0 && index[i][0] == 0) continue;
index[i].pb(0);
}
}
rep(i,h) index[i].pb(w);
rep(i,w+1) index[h].pb(i);
rep(i,h+1) sort(ALL(index[i]));
/*rep(i,h+1){
rep(j,(int)index[i].size()){
cout << index[i][j] << " ";
}
cout << endl;
}*/
vector<int> index2[w+1];
rep(i,h+1){
rep(j,(int)index[i].size()){
index2[index[i][j]].pb(i);
}
}
rep(i,w+1) sort(ALL(index2[i]));
BIT<ll> cnt(h+5);
ll ans = 0;
rep(i,h) ans += (ll)index[i][0];
rep(i,index[0][0]){
ans += cnt.sum(1,(ll)index2[i][0]+1LL);
for(int j : index2[i]){
if(cnt.sum(j+1, j+2) == 0) cnt.add(j+1,j+2,1LL);
}
}
cout << ans << endl;
}
| //#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
//#include<ctime>
//#include<algorithm>//next_permutation
#define rep(i,n) for (ll i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
const ll MOD = 1000000007;
const ll MAX = 2000001;
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);
}
int main(){
ll n,m; cin >> n >> m;
vl gold(n);
rep(i,n) cin >> gold[i];
vector<set<ll>> e(n);
rep(i,m){
ll a,b; cin >> a >> b; --a; --b;
e[a].insert(b);
}
vl dp(n,1e18); //その街に持ってこられる最安の金の価格
rep(i,n){
for(auto a : e[i]){
//cout << i << a << endl;
dp[a] = min({dp[i],dp[a],gold[i]});
}
}
//cout << "aaaaa" << endl;
ll ans = -1e18;
rep(i,n){
ans = max(ans,gold[i] - dp[i]);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> ans;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ans.emplace_back(i);
if (n != i * i) ans.emplace_back(n / i);
}
}
sort(ans.begin(), ans.end());
for (const long long &v: ans)
cout << v << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define yn {puts("Yes");}else{puts("No");}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
bool isi(const string&s){for(int l=0,r=sz(s)-1;l<r;++l,--r)if(s[l]!=s[r])return false;return true;}
int main() {
int a;
cin>> a;
cout<<100-(a%100)<<endl;
return 0;
} |
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>
const ll mod = 1000000007;
const int INF = 1001001001;
struct mint {
ll x;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
istream& operator>>(istream& is, const mint& a){ return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a){ return os << a.x; }
set<char> s;
map<char, int> mp, mp_inv;
int C(char c){
int num = mp[c];
int res = 0;
rep(i, num){
if(s.find(mp_inv[i]) == s.end())res++;
}
return res;
}
int main(){
string n;
int k;
cin >> n >> k;
int keta = 0;
for (int i = 0; n[i] != 0; i++) keta++;
rep(i, 10) {
mp[(char)('0'+i)] = i;
mp_inv[i] = '0' + i;
}
rep(i, 6) {
mp[(char)('A'+i)] = i + 10;
mp_inv[i+10] = 'A' + i;
}
vector<vector<vector<mint> > > dp(keta+1, vector<vector<mint> >(k+1, vector<mint>(2)));
dp[0][0][0] = 1;
rep(i, keta){
dp[i+1][0][0] = 0;
dp[i+1][0][1] = 1;
rep(j, k){
if(s.find(n[i]) == s.end()) dp[i+1][j+1][0] = dp[i][j][0];
else dp[i+1][j+1][0] = dp[i][j+1][0];
int c = C(n[i]);
if(j != 0){
if(i == 0) dp[i+1][j+1][1] = dp[i][j][0] * (c-1) + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j) + dp[i][j+1][1] * (j+1);
else dp[i+1][j+1][1] = dp[i][j][0] * c + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j) + dp[i][j+1][1] * (j+1);
}else{
if(i == 0) dp[i+1][j+1][1] = dp[i][j][0] * (c-1) + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j-1) + dp[i][j+1][1] * (j+1);
else dp[i+1][j+1][1] = dp[i][j][0] * c + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j-1) + dp[i][j+1][1] * (j+1);
}
}
s.insert(n[i]);
}
// rep(j, k+1){
// rep(i, keta+1){
// cout << dp[i][j][0] << " ";
// }cout << endl;
// }cout << endl;
// rep(j, k+1){
// rep(i, keta+1){
// cout << dp[i][j][1] << " ";
// }cout << endl;
// }
mint ans;
ans = dp[keta][k][0] + dp[keta][k][1];
cout << ans << endl;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
ll gcd(ll n, ll m)
{
ll tmp;
while (m != 0)
{
tmp = n % m;
n = m;
m = tmp;
}
return n;
}
ll lcm(ll n, ll m)
{
return abs(n) / gcd(n, m) * abs(m); //gl=xy
}
using namespace std;
//using namespace atcoder;
template<int mod>
struct Modint{
int x;
Modint():x(0){}
Modint(int64_t y):x((y%mod+mod)%mod){}
Modint &operator+=(const Modint &p){
if((x+=p.x)>=mod)
x -= mod;
return *this;
}
Modint &operator-=(const Modint &p){
if((x+=mod-p.x)>=mod)
x -= mod;
return *this;
}
Modint &operator*=(const Modint &p){
x = (1LL * x * p.x) % mod;
return *this;
}
Modint &operator/=(const Modint &p){
*this *= p.inverse();
return *this;
}
Modint operator-() const { return Modint(-x); }
Modint operator+(const Modint &p) const{
return Modint(*this) += p;
}
Modint operator-(const Modint &p) const{
return Modint(*this) -= p;
}
Modint operator*(const Modint &p) const{
return Modint(*this) *= p;
}
Modint operator/(const Modint &p) const{
return Modint(*this) /= p;
}
bool operator==(const Modint &p) const { return x == p.x; }
bool operator!=(const Modint &p) const{return x != p.x;}
Modint inverse() const{//非再帰拡張ユークリッド
int a = x, b = mod, u = 1, v = 0;
while(b>0){
int t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return Modint(u);
}
Modint pow(int64_t n) const{//繰り返し二乗法
Modint ret(1), mul(x);
while(n>0){
if(n&1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os,const Modint &p){
return os << p.x;
}
friend istream &operator>>(istream &is, Modint &p){
int64_t t;
is >> t;
p=Modint<mod>(t);
return is;
}
};
using modint = Modint<mod>;
void solve(){
ll t;
cin>>t;
while(t--){
ll n,a,b;
cin>>n>>a>>b;
if(n<a+b){
cout<<0<<"\n";
continue;
}
auto f=[&](modint x)->modint{
return x*x;
};
modint ans=f(n-a+1)*f(n-b+1);
modint x=min(a,b)-1;
ans-=f(x)*f(x+3+2*n-2*(a+b))/4*4;//縦も横も完全にはかぶらない
modint s=max(a,b);
ans-=(s-x)*x*(-s+n+1)*(x+3+2*n-2*(a+b))*2;
ans-=f(s-x)*f(-s+n+1);//縦も横も完全にかぶる
cout<<ans<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
|
//always use
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long ll;
#define MOD 1000000007
#define HASHMOD 1275341791
//2 147 483 647 (2^31-1) int max
//9 223 372 036 854 775 807 (2^63-1) ll max
//codeforces and USACO
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);}
//USACO
void file_io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();}
//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 << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
//main code
int main(){
fast_io();
int n,m;
cin >> n >> m;
vector<vector<int>>a(n,vector<int>(m));
forn(i,n){
string s;
cin >> s;
forn(j,m){
if (s.at(j)=='.'){
a[i][j]=1;
}
}
}
vector<ll>diag(n+m-1,0);
vector<ll>vert(m,0);
forn(i,n){
ll hor=0;
forn(j,m){
if (a[i][j]==1){
ll x=0;
if (i==0 and j==0){
x++;
}
x=(x+diag[i-j+m-1])%MOD;
x=(x+hor)%MOD;
x=(x+vert[j])%MOD;
if (i==n-1 and j==m-1){
cout << x <<" ";
}
diag[i-j+m-1]=(diag[i-j+m-1]+x)%MOD;
hor=(x+hor)%MOD;
vert[j]=(x+vert[j])%MOD;
}
else{
diag[i-j+m-1]=0;
hor=0;
vert[j]=0;
}
}
}
}
| #include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
// auto mod int
// depends on Template
const int mod = 1000000007;
//const int mod = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
long long modpow(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
char s[2010][2010];
mint tate[2010][2010];
mint yoko[2010][2010];
mint naname[2010][2010];
mint dp[2010][2010];
int main(){
ll h,w;
cin >> h >> w;
rep(i,h)rep(j,w)cin>>s[i+1][j+1];
dp[1][1]=1;
tate[1][1]=1;
yoko[1][1]=1;
naname[1][1]=1;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(i==1&&j==1)continue;
if(s[i][j]=='#')continue;
dp[i][j]+=tate[i-1][j];
dp[i][j]+=yoko[i][j-1];
dp[i][j]+=naname[i-1][j-1];
tate[i][j]=dp[i][j]+tate[i-1][j];
yoko[i][j]=dp[i][j]+yoko[i][j-1];
naname[i][j]=dp[i][j]+naname[i-1][j-1];
}
}
cout << dp[h][w]<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int maxk = 302;
const long long mod = 998244353;
long long add_mod (long long a, long long b) {
return (a + b) % mod;
}
long long sub_mod (long long a, long long b) {
return (a + mod - b) % mod;
}
long long mul_mod (long long a, long long b) {
return (a * b) % mod;
}
long long pow_mod (long long a, long long b) {
long long res = 1LL, t = a;
while (b) {
if (b & 1)
res = mul_mod(res, t);
t = mul_mod(t, t);
b >>= 1;
}
return res;
}
long long div_mod (long long a, long long b) {
return mul_mod(a, pow_mod(b, mod - 2));
}
long long ncr_mod[maxk][maxk];
void precalc () {
for (int n = 0; n < maxk; ++n) {
for (int r = 0; r < maxk; ++r) {
if (n == 0 || r == 0 || n == r)
ncr_mod[n][r] = 1LL;
else
ncr_mod[n][r] = add_mod(ncr_mod[n - 1][r], ncr_mod[n - 1][r - 1]);
}
}
}
long long a[maxn];
long long s[maxk], diff[maxk];
int32_t main () {
precalc();
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
long long cur = 1LL, cur_d = 1LL;
for (int x = 0; x <= k; ++x) {
s[x] = add_mod(s[x], cur);
diff[x] = add_mod(diff[x], cur_d);
cur = mul_mod(cur, a[i]);
cur_d = mul_mod(cur_d, mul_mod(2LL, a[i]));
}
}
for (int x = 1; x <= k; ++x) {
long long ans = 0LL;
for (int j = 0; j <= x; ++j)
ans = add_mod(ans, mul_mod(s[x - j], mul_mod(ncr_mod[x][j], s[j])));
ans = div_mod(sub_mod(ans, diff[x]), 2LL);
printf("%lld\n", ans);
}
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include<bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class EPeddler {
public:
vector<int> graph[200005];
void solve(istream& cin, ostream& cout) {
SPEED;
int n, m; cin >> n >> m;
vector<ll> a(n);
rep(i, n) {
cin >> a[i];
}
vector<int> x(m), y(m);
rep(i, m) {
cin >> x[i] >> y[i];
x[i]--; y[i]--;
graph[x[i]].push_back(y[i]);
}
vector<ll> dp(n, LLONG_MIN/2);
vector<ll> lowest(n, LLONG_MAX/2);
for(int i=0; i<n; i++) {
for(auto e: graph[i]) {
chmin(lowest[e], lowest[i]);
chmin(lowest[e], a[i]);
}
}
ll ret = LLONG_MIN/2;
for(int i=0; i<n; i++) {
chmax(ret, a[i] - lowest[i]);
}
cout << ret << endl;
}
};
signed main() {
EPeddler solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
} |
///****In the name of ALLAH, most Gracious, most Compassionate****//
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)
#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl
template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
os<<"{"<<p.first<<","<<p.second<<"}";
return os;
}
const int N=3e5+5;
const int oo=1e9+7;
int a[N];
int32_t main()
{
FastIO;
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
priority_queue < pair < int , int > > pq;
for(int i=0;i<k;i++)
pq.push({0,i});
ll ans = 0;
for(int i=0;i<n and ! pq.empty();i++)
{
while(!pq.empty())
{
auto x = pq.top();
pq.pop();
int now = - x.first ;
// DBG(now);
// DBG(a[i]);
// NL;
if(a[i] < now)
{
pq.push(x);
break;
}
else if(a[i] == now)
{
pq.push({ - (now +1) , x.second});
break;
}
else
{
ans+=now;
continue ;
}
}
}
while(!pq.empty())
{
ans += -pq.top().first;
pq.pop();
}
cout<<ans<<"\n";
}
| /* B */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 3e5 + 10;
int n, m, a[N], s[N];
ll solve(void)
{
int num = m;
ll res = 0;
for (int k = 1; k <= n; ++k) s[a[k]]++;
for (int k = 0; k <= n; ++k) {
if (s[k] >= num) continue;
res += (ll)(num - s[k]) * k;
num = s[k];
}
return res;
}
signed main(int argc, char** argv)
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while (cin >> n >> m) {
for (int k = 1; k <= n; ++k) cin >> a[k];
sort(a + 1, a + n + 1);
cout << solve() << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
char ch = getchar(); x = 0; int f = 0;
while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); }
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
if (f) x = -x;
}
int n, x, y, f[200], s[200], tp;
int res[200], cnt;
signed main() {
read (n); f[0] = f[1] = 1;
for (int i = 2; i <= 87; ++i)
f[i] = f[i - 1] + f[i - 2];
int t = n;
for (int i = 86; i; --i) {
if (t >= f[i]) {
++tp, s[tp] = i, t -= f[i];
}
}
// if (t) puts ("666666");
// for (int i = 1; i <= tp; ++i) printf ("%lld ", s[i]); puts ("");
int tag = (s[1] & 1); s[tp + 1] = s[1];
for (int i = tp; i; --i) s[i] = s[1] - s[i];
// for (int i = 1; i <= tp; ++i) printf ("%lld ", s[i]); puts ("");
if (tag) {
res[++cnt] = 2;
int now = 1; // 1 x 0 y
for (int i = 2; i <= tp + 1; ++i) {
for (int j = s[i - 1] + 1; j <= s[i]; ++j) {
res[++cnt] = now ? 3 : 4, now ^= 1;
}
if (i <= tp) res[++cnt] = now ? 2 : 1;
}
} else {
res[++cnt] = 1;
int now = 0; // 1 x 0 y
for (int i = 2; i <= tp + 1; ++i) {
for (int j = s[i - 1] + 1; j <= s[i]; ++j) {
res[++cnt] = now ? 3 : 4, now ^= 1;
}
if (i <= tp) res[++cnt] = now ? 2 : 1;
}
}
int x = 0, y = 0;
printf ("%lld\n", cnt);
for (int i = 1; i <= cnt; ++i) {
printf ("%lld ", res[i]);
// if (res[i] == 1) ++x;
// if (res[i] == 2) ++y;
// if (res[i] == 3) x = x + y;
// if (res[i] == 4) y = x + y;
// printf ("%lld %lld %lld %lld\n", i, res[i], x, y);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(v) (v).begin(),(v).end()
using pii = pair<int, int>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T>
struct SegmentTree{
using OP = function<T(T,T)>;
int n, defn;
vector<T> node;
T t;
OP op;
SegmentTree(int n_, T t, OP op) : t(t),op(op){
n = 1;
defn = n_;
while(n < n_) n<<=1;
node.assign(2*n,t);
}
void init(int n_){
n = 1;
defn = n_;
while(n < n_) n<<=1;
node.assign(2*n,t);
}
void set(int k,T x){
node.at(k+n) = x;
}
void build(){
for(int k = n - 1; k > 0; k--){
node.at(k) = op(node.at(2*k),node.at(2*k+1));
}
}
void change(int k, T x){
k += n;
node.at(k) = x;
while(k >>= 1){
node.at(k) = op(node.at(2*k),node.at(2*k+1));
}
}
void change(int l, int r, T x){
for(int k = l; k < r; k++) change(k,x);
}
template<typename C>
void update(int k, C c){
T x = node.at(k+n);
T x2 = c(x);
change(k, x2);
}
template<typename C>
void update(int l, int r, C c){
for(int k = l; k < r; k++) update(k,c);
}
T at(int k){
return node.at(k+n);
}
void add(int k, T a){
T x = at(k)+a;
change(k,x);
}
T query(int a, int b){
if(a==0 && b==defn) return all_query();
T L = t, R = t;
for(a += n, b += n; a < b; a >>= 1, b >>= 1) {
if(a & 1) L = op(L, node.at(a++));
if(b & 1) R = op(node.at(--b), R);
}
return op(L, R);
}
T all_query() { return node.at(1); }
template<typename C>
int max_right(int l, C &check){
if(l == defn) return defn;
l += n;
T tm = t;
do{
while(l%2 == 0) l >>= 1;
if(!check(op(tm, node.at(l)))){
while(l < n){
l *= 2;
if(check(op(tm, node.at(l)))){
tm = op(tm, node.at(l));
l++;
}
}
return l - n;
}
tm = op(tm, node.at(l));
l++;
}while((l&-l) != l);
return defn;
}
template<typename C>
int max_left(int r, C &check){
if(r == 0) return 0;
r += n;
T tm = t;
do{
r--;
while(r>1 && (r%2)) r >>= 1;
if(!check(op(node.at(r), tm))){
while(r < n){
r = 2*r + 1;
if(check(op(node.at(r), tm))){
tm = op(node.at(r), tm);
r--;
}
}
return r + 1 - n;
}
tm = op(node.at(r), tm);
}while((r&-r) != r);
return 0;
}
};
int main(){
int H, W, M; cin >> H >> W >> M;
vector<int> height(W, H), width(H, W);
vector<vector<int>> ps(H);
rep(i, M){
int x, y; cin >> x >> y; x--; y--;
chmin(height[y], x); chmin(width[x], y);
ps[x].emplace_back(y);
}
ll ans = 0;
SegmentTree<int> seg(
W,
0,
[](int a, int b) { return a+b; }
);
rep(i, W){
if(i < width[0]) ans += height[i];
else seg.set(i, 1);
}
seg.build();
rep(i, height[0]){
for(auto&&y : ps[i]){
seg.change(y, 1);
}
ans += seg.query(0, width[i]);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
ll N = 0;
cin >> N;
set<ll> A;
ll t;
ll x = 1000000000000;
for(ll i = 0; i < N; i++) {
cin >> t;
A.insert(t);
x = min(x, t);
}
while(A.size() != 1) {
set<ll> ans;
ans.clear();
for (set<ll>::iterator it = A.begin(); it != A.end(); it++) {
ans.insert((*it)%x + x);
//cout << *it << endl;
}
ll memo = x;
for (set<ll>::iterator it = ans.begin(); it != ans.end(); it++) {
if(*it != memo)x = min(x, *it - memo);
}
A.clear();
A = ans;
//cout << "##" << endl;
}
cout << x << endl;
return 0;
} | #include<iostream>
#include<bits/stdc++.h>
#include<climits>
#define PI 3.14159265358979323846264338327950L
#define ll long long
#define db double
using namespace std;
const int MOD = 1e9+7;
void go(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout<<fixed;
cout<<setprecision(20);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void solve(){
int n; cin>>n;
int arr[n];
for(int i = 0; i < n; i++) cin>>arr[i];
sort(arr,arr+n);
int sol = 0;
for(int i = 0; i < n; i++){
sol = __gcd(sol,arr[i]);
}
cout<<sol<<endl;
}
int main(){
go();
// code here
int t = 1;
while(t--) solve();
}
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 1e9+7 ;
long double pie = acos(-1) ;
ll INF = 1e18 ;
void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
ll extGCD(ll a,ll b,ll &x,ll &y){
if(b==0){
x = 1 ;
y = 0 ;
return a ;
}
ll d = extGCD(b,a%b,y,x) ;
y -= a/b*x ;
return d ;
}
void fix_cout(){cout << fixed << setprecision(20) ;}
template<class T> void chmax(T &a,T b){if(a<b) a = b ;}
template<class T> void chmin(T &a,T b){if(a>b) a = b ;}
string ans = "" ;
int mx = 0 ;
VV<int> a(50,V<int>(50)) ;
VV<int> t(50,V<int>(50)) ;
VV<bool> tt(50,V<bool>(50,true)) ;
chrono::_V2::system_clock::time_point start,now ;
void DFS(int y,int x,int sum,string &cur){
now = chrono::system_clock::now() ;
if(chrono::duration_cast<chrono::milliseconds>(now-start).count()>1000) return ;
sum += a[y][x] ;
if(mx<sum){
mx = sum ;
ans = cur ;
}
tt[y][x] = false ;
if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = false ;
if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = false ;
if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = false ;
if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = false ;
cur += 'U' ;
if(y>0&&tt[y-1][x]) DFS(y-1,x,sum,cur) ;
cur.pop_back() ; cur += 'D' ;
if(y+1<50&&tt[y+1][x]) DFS(y+1,x,sum,cur) ;
cur.pop_back() ; cur += 'L' ;
if(x>0&&tt[y][x-1]) DFS(y,x-1,sum,cur) ;
cur.pop_back() ; cur += 'R' ;
if(x+1<50&&tt[y][x+1]) DFS(y,x+1,sum,cur) ;
cur.pop_back() ;
tt[y][x] = true ;
if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = true ;
if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = true ;
if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = true ;
if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = true ;
}
void DFS2(int y,int x,int sum,string &cur){
now = chrono::system_clock::now() ;
if(chrono::duration_cast<chrono::milliseconds>(now-start).count()>1970) return ;
sum += a[y][x] ;
if(mx<sum){
mx = sum ;
ans = cur ;
}
tt[y][x] = false ;
if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = false ;
if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = false ;
if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = false ;
if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = false ;
cur += 'R' ;
if(x+1<50&&tt[y][x+1]) DFS2(y,x+1,sum,cur) ;
cur.pop_back() ; cur += 'L' ;
if(x>0&&tt[y][x-1]) DFS2(y,x-1,sum,cur) ;
cur.pop_back() ; cur += 'D' ;
if(y+1<50&&tt[y+1][x]) DFS2(y+1,x,sum,cur) ;
cur.pop_back() ; cur += 'U' ;
if(y>0&&tt[y-1][x]) DFS2(y-1,x,sum,cur) ;
cur.pop_back() ;
tt[y][x] = true ;
if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = true ;
if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = true ;
if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = true ;
if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = true ;
}
int main(){
start = chrono::system_clock::now() ;
int sx,sy ;
cin >> sy >> sx ;
for(auto &i:t) for(auto &j:i) cin >> j ;
for(auto &i:a) for(auto &j:i) cin >> j ;
int sum = 0 ;
string cur = "" ;
DFS(sy,sx,sum,cur) ;
sum = 0 ;
cur = "" ;
for(int i=0;i<50;i++) for(int j=0;j<50;j++) tt[i][j] = true ;
DFS2(sy,sx,sum,cur) ;
cout << ans << endl ;
} | #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
#define irep(i,n) for(int i = n-1; i >= 0; --i )
using namespace std;
using ll = long long;
using P = pair<int,int>;
const int N=1505;
int v[N][N];
int main() {
int h,w,n,m;
cin >> h >> w >> n >> m;
rep(i,n){
int a,b;
cin >> a >> b;
v[a-1][b-1] = 2;
}
rep(i,m){
int a,b;
cin >> a >> b;
v[a-1][b-1] = -1;
}
bool b;
rep(i,h){
b=false;
rep(j,w){
if(v[i][j]==-1) b = false;
if(v[i][j]==2) b = true;
if(v[i][j]==0 && b) v[i][j] = 1;
}
b=false;
irep(j,w){
if(v[i][j]==-1) b = false;
if(v[i][j]==2) b = true;
if(v[i][j]==0 && b) v[i][j] = 1;
}
}
rep(j,w){
b=false;
rep(i,h){
if(v[i][j]==-1) b = false;
if(v[i][j]==2) b = true;
if(v[i][j]==0 && b) v[i][j] = 1;
}
b=false;
irep(i,h){
if(v[i][j]==-1) b = false;
if(v[i][j]==2) b = true;
if(v[i][j]==0 && b) v[i][j] = 1;
}
}
int ans = 0;
rep(i,h)rep(j,w) if(v[i][j]>0) ++ans;
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define lop(i,n) for(ll i=0;i<n;i++)
#define lop1(i,n) for(ll i=1;i<=n;i++)
#define lopr(i,n) for(ll i=n-1;i>=0;i--)
#define ll long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define F first
#define S second
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define LCM(a,b) (a*b)/__gcd(a,b)
#define mii map<int,int>
#define mll map<ll,ll>
#define ub upper_bound
#define lb lower_bound
#define sz(x) (ll)x.size()
#define ld long double
#define pcnt(x) __builtin_popcountll(x)
const long long I1=1e9;
const long long I2=1e18;
const int32_t M1=1e9+7;
const int32_t M2=998244353;
template<typename T,typename T1>T maxn(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T minn(T &a,T1 b){if(b<a)a=b;return a;}
void solve()
{
//code begins from here//
int n,m;
cin>>n>>m;
int a[n][n];
mem(a,0);
lop(i,m){
int x,y;
cin>>x>>y;
x--;y--;
a[x][y]=1;
a[y][x]=1;
}
int N = 1<<n;
vector <bool> v(N,false);
for (int i =1 ; i<N; i++){
bool cc = true;
lop(k,n){
for (int j=k+1; j<n; j++){
if((i&(1<<j)) && (i&(1<<k))){
if(!a[j][k]) cc=false;
}
}
}
v[i]=cc;
}
vector <int> dp(N,I1);
dp[0]=0;
for (int i=1 ; i<N; i++){
for (int j=i; j; j=(j-1)&i){
if(v[j]) minn(dp[i],dp[i^j]+1);
}
}
cout<<dp[N-1]<<endl;
}
signed main()
{
IOS;
#ifdef MODULO
initialize();
#endif
#ifdef SIEVE
sieve();
#endif
int testcase=1;
// cin>>testcase;
while(testcase--) solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define LL long long
int n;
LL m,f[202][202][202],a[2020];
#define INF (1LL<<60)
int main(){
scanf("%d%lld",&n,&m);
for (int i=1; i<=n; i++) scanf("%lld",&a[i]);
LL ans = m;
for (int mod=1; mod<=n; mod++){
memset(f, -1, sizeof(f));
f[0][0][0] = 0;
for (int i=0; i<n; i++){
for (int j=0; j<=i; j++){
for (int k=0; k<mod; k++){
f[i+1][j][k] = max(f[i+1][j][k], f[i][j][k]);
if (f[i][j][k] == -1) continue;
int p = (k + a[i+1])%mod;
f[i+1][j+1][p] = max(f[i+1][j+1][p], f[i][j][k] + a[i+1]);
}
}
}
LL p = m%mod;
// printf("%d %d %d %lld\n",n, mod, p, f[n][mod][p]);
if (f[n][mod][p]!=-1) ans = min(ans, (m - f[n][mod][p])/mod);
}
printf("%lld\n",ans);
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), b(n), c(n);
rep(i, n)cin >> a[i];
rep(i, n)cin >> b[i];
rep(i, n)cin >> c[i];
unordered_map<int, int> mp;
rep(i, n)++mp[a[i]];
long long ans = 0;
rep(i, n) {
ans += mp[b[c[i] - 1]];
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
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; }
// Mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
ll x;
mint():x(0){}
mint(ll x):x((x%mod+mod)%mod){}
// mint(ll x):x(x){}
mint& fix() { x = (x%mod+mod)%mod; return *this;}
mint operator-() const { return mint(0) - *this;}
mint& operator+=(const mint& a){ if((x+=a.x)>=mod) x-=mod; return *this;}
mint& operator-=(const mint& a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
mint& operator*=(const mint& a){ (x*=a.x)%=mod; return *this;}
mint operator+(const mint& a)const{ return mint(*this) += a;}
mint operator-(const mint& a)const{ return mint(*this) -= a;}
mint operator*(const mint& a)const{ return mint(*this) *= a;}
bool operator<(const mint& a)const{ return x < a.x;}
bool operator==(const mint& a)const{ return x == a.x;}
};
istream& operator>>(istream&i,mint&a){i>>a.x;return i;}
ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;}
//
ll gcd(ll a, ll b){
if(a % b == 0) return b;
else return gcd(b, a%b);
}
int main() {
int n;
cin >> n;
vi a(n);
rep(i, n) cin >> a[i];
int ans = a[n-1];
rep(i, n-1) {
ans = gcd(ans, a[i]);
}
cout << ans << endl;
} |
#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;
// #pragma GCC optimize("O2")
typedef long long int ll;
typedef long double ld;
typedef map <ll,ll> mm;
typedef vector <ll> mv;
typedef pair <ll,ll> mp;
typedef set <ll> ms;
typedef multiset <ll> mms;
typedef queue <ll> mq;
typedef deque <ll> mdq;
typedef stack <ll> mst;
typedef priority_queue <ll> mpq;
typedef priority_queue <ll, vector<ll>, greater<ll> > mmh;
typedef complex<double> cd;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(),v.end()
#define deb(x) cout << #x << " " << x << endl;
#define ff first
#define ss second
#define pb(v) push_back(v)
#define lb(v) lower_bound(v)
#define ub(v) upper_bound(v)
#define cn continue
#define forl(i,n) for(ll i=0;i<n;i++)
#define forlr(i,n) for(ll i=n-1;i>=0;i--)
#define rev(s) reverse(s.begin(),s.end())
void swap(ll &a,ll &b) {
ll tmp=a;
a=b;
b=tmp;
}
ll atoistring(string s) {
stringstream lol(s);
ll x;
lol>>x;
return x;
}
void pv(mv v) {
forl(i,v.size()) cout<<v[i]<<" ";
cout<<"\n";
}
void pa(ll *v, ll size) {
forl(i,size) cout<<v[i]<<" ";
cout<<"\n";
}
void removeDups(mv &v) {
sort(all(v));
mv::iterator it = unique(v.begin(), v.end());
v.resize(distance(v.begin(), it));
}
inline ld kthRoot(ld n, ll k) {
return pow(k, (1.0 / k) * (log(n) / log(k)));
}
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;
}
void checkmod(ll &x, ll mod) {
if(x>=mod) x%=mod;
}
ll mod = 1e9 + 7;
ll arr[200005], pre[200005], maxi[200005];
void thisIsMain() {
ll n;
cin>>n;
forl(i,n) {
cin>>arr[i];
pre[i] = arr[i];
}
maxi[0] = arr[0];
forl(i,n - 1) {
pre[i + 1] += pre[i];
maxi[i + 1] = max(maxi[i], pre[i + 1]);
}
ll ans = 0;
ll pos = 0;
forl(i,n) {
ans = max(ans, pos + maxi[i]);
pos += pre[i];
}
cout<<ans<<"\n";
}
int main(void)
{
fastio
ll t = 1;
// cin>>t;
while(t--) thisIsMain();
return 0;
} | #include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<functional>
#include<string>
#include<numeric>
#include<cstring>
#include<random>
#include<array>
#include<fstream>
#include<iomanip>
#include<list>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<queue>
/*
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
*/
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,a,b) for(ll i = a; i < b; ++i)
#define PRI(s) std::cout << s << endl
#define PRIF(v, n) printf("%."#n"f\n", (double)v)
template<typename A, typename B>void mins(A& a, const B& b) { a = min(a, (A)b); };
template<typename A, typename B>void maxs(A& a, const B& b) { a = max(a, (A)b); };
vector<vector<bool>> S;
vector<vector<vector<pair<int,int>>>> E;
vector<vector<int>> F;
void dfs(int i, int j, int c) {
F[i][j] = c;
for (auto p : E[i][j]) if(F[p.first][p.second]==-1)dfs(p.first, p.second, c);
}
int main() {
ll H, W; cin >> H >> W;
S.resize(H, vector<bool>(W));
E.resize(H, vector<vector<pair<int, int>>>(W));
F.resize(H, vector<int>(W, -1));
REP(i, 0, H) REP(j, 0, W) {
char c; cin >> c;
S[i][j] = (c == '#');
}
S[0][0] = true;
S[0][W - 1] = true;
S[H - 1][0] = true;
S[H - 1][W - 1] = true;
REP(i, 0, H) {
int last = -1;
REP(j, 0, W) {
if (!S[i][j]) continue;
if (last == -1) {
last = j;
continue;
}
E[i][j].push_back({ i,last });
E[i][last].push_back({ i,j });
last = j;
}
}
REP(j, 0, W) {
int last = -1;
REP(i, 0, H) {
if (!S[i][j]) continue;
if (last == -1) {
last = i;
continue;
}
E[i][j].push_back({ last,j });
E[last][j].push_back({ i,j });
last = i;
}
}
int fm = -1;
REP(i, 0, H) REP(j, 0, W) {
if (!S[i][j] || F[i][j] != -1)continue;
dfs(i, j, ++fm);
}
int emptycol = 0;
int emptyrow = 0;
REP(i, 0, H) {
bool ok = true;
REP(j, 0, W) {
if (S[i][j]) {
ok = false;
break;
}
}
if (ok)++emptycol;
}
REP(j, 0, W) {
bool ok = true;
REP(i, 0, H) {
if (S[i][j]) {
ok = false;
break;
}
}
if (ok)++emptyrow;
}
PRI(fm + min(emptycol, emptyrow));
return 0;
}
|
#include<iostream>
#include<vector>
#include<cstring>
#include<map>
#include<bitset>
#include<assert.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<chrono>
#include<stack>
#include<deque>
#include<random>
#define long long long
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N=1e6,inf=1e9,mod=1e9+7;
struct abcd
{
int tr;
struct treap
{
int lt,rt,dt,pri,sz;
long sm;
}bst[N+2];
void cal(int nd)
{
int l=bst[nd].lt,r=bst[nd].rt;
bst[nd].sz=bst[l].sz+bst[r].sz+1;
bst[nd].sm=bst[l].sm+bst[r].sm+bst[nd].dt;
}
//left tree will contain value <= vl.
pair<int,int>splitbyValue(int nd,int vl)
{
if(!nd)return {0,0};pair<int,int>p;
if(bst[nd].dt<=vl)
{
p=splitbyValue(bst[nd].rt,vl);
bst[nd].rt=p.first;cal(nd);
return {nd,p.second};
}
else
{
p=splitbyValue(bst[nd].lt,vl);
bst[nd].lt=p.second;cal(nd);
return {p.first,nd};
}
}
pair<int,int>splitbySize(int nd,int ss)
{
if(!nd)return {0,0};pair<int,int>p;
if(bst[bst[nd].lt].sz>=ss)
{
p=splitbySize(bst[nd].lt,ss);
bst[nd].lt=p.second;cal(nd);
return {p.first,nd};
}
else
{
p=splitbySize(bst[nd].rt,ss-bst[bst[nd].lt].sz-1);
bst[nd].rt=p.first;cal(nd);
return {nd,p.second};
}
}
//lt and rt are bst and max element of lt <= minimum of rt
int join(int lt,int rt)
{
if(!lt)return rt;if(!rt)return lt;
if(bst[lt].pri>=bst[rt].pri)
{
bst[lt].rt=join(bst[lt].rt,rt);
cal(lt);return lt;
}
else
{
bst[rt].lt=join(lt,bst[rt].lt);
cal(rt);return rt;
}
}
int add(int rt,int vl)
{
pair<int,int>p=splitbyValue(rt,vl);
bst[++tr]={0,0,vl,rng()%inf,1,vl};
rt=join(p.first,tr);
rt=join(rt,p.second);
return rt;
}
int erase(int rt,int vl)
{
pair<int,int>p=splitbyValue(rt,vl-1);
pair<int,int>p2=splitbySize(p.second,1);
return join(p.first,p2.second);
}
}lft,rgt;
int a[N+2],b[N+2];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m,q;cin>>n>>m>>q;
long ans=0;int rt1=0,rt2=0;
for(int i=1;i<=n;i++)rt1=lft.add(rt1,0);
for(int i=1;i<=m;i++)rt2=rgt.add(rt2,0);
while(q--)
{
int ty;cin>>ty;
if(ty==1)
{
int x,y;cin>>x>>y;
pair<int,int>p=rgt.splitbyValue(rt2,a[x]);
ans-=(1LL*rgt.bst[p.first].sz*a[x]+rgt.bst[p.second].sm);
rt2=rgt.join(p.first,p.second);
rt1=lft.erase(rt1,a[x]);
a[x]=y;
p=rgt.splitbyValue(rt2,a[x]);
ans+=(1LL*rgt.bst[p.first].sz*a[x]+rgt.bst[p.second].sm);
rt2=rgt.join(p.first,p.second);
rt1=lft.add(rt1,a[x]);
}
else
{
int x,y;cin>>x>>y;
pair<int,int>p=lft.splitbyValue(rt1,b[x]);
ans-=(1LL*lft.bst[p.first].sz*b[x]+lft.bst[p.second].sm);
rt1=lft.join(p.first,p.second);
rt2=rgt.erase(rt2,b[x]);
b[x]=y;
p=lft.splitbyValue(rt1,b[x]);
ans+=(1LL*lft.bst[p.first].sz*b[x]+lft.bst[p.second].sm);
rt1=lft.join(p.first,p.second);
rt2=rgt.add(rt2,b[x]);
}
cout<<ans<<'\n';
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
const int N = 2e5 + 5;
const int INF = 2e9;
const ll LINF = 9e18;
struct query {
int T, X, Y;
};
int n, m, q;
int a[N], b[N];
ll tree[4][N << 2];
// 0 = Sum_A, 1 = Sum_B, 2 = Freq_A, 3 = Freq_B
void update(int idx, int l, int r, int pos, ll val, int d) {
if(l > pos || r < pos) return;
if(l == r) {
tree[d][idx] += val;
return;
}
int mid = l + r >> 1;
update(idx << 1, l, mid, pos, val, d);
update(idx << 1 | 1, mid + 1, r, pos, val, d);
tree[d][idx] = tree[d][idx << 1] + tree[d][idx << 1 | 1];
}
ll rangeSum(int idx, int l, int r, int from, int to, int d) {
if(l > to || r < from) return 0;
if(from <= l && r <= to) return tree[d][idx];
int mid = l + r >> 1;
return (
rangeSum(idx << 1, l, mid, from, to, d) +
rangeSum(idx << 1 | 1, mid + 1, r, from, to, d)
);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
vector<query> Q;
set<int> distinct;
distinct.insert(0);
while(q--) {
int t, x, y; cin >> t >> x >> y;
Q.push_back({t, x, y});
distinct.insert(y);
}
int cnt = 0;
map<int, int> idx;
for(int u : distinct) {
idx[u] = ++cnt;
}
int s = distinct.size();
update(1, 1, s, idx[0], n, 2);
update(1, 1, s, idx[0], m, 3);
ll ans = 0;
for(query u : Q) {
if(u.T == 1) {
int prv = idx[a[u.X]];
int nxt = idx[u.Y];
ans -= rangeSum(1, 1, s, 1, prv, 3) * a[u.X];
ans += rangeSum(1, 1, s, 1, prv, 1);
ans -= rangeSum(1, 1, s, 1, nxt, 1);
ans += rangeSum(1, 1, s, 1, nxt, 3) * u.Y;
update(1, 1, s, prv, -a[u.X], 0);
update(1, 1, s, nxt, u.Y, 0);
update(1, 1, s, prv, -1, 2);
update(1, 1, s, nxt, 1, 2);
a[u.X] = u.Y;
} else {
int prv = idx[b[u.X]];
int nxt = idx[u.Y];
ans -= rangeSum(1, 1, s, 1, prv, 2) * b[u.X];
ans += rangeSum(1, 1, s, 1, prv, 0);
ans -= rangeSum(1, 1, s, 1, nxt, 0);
ans += rangeSum(1, 1, s, 1, nxt, 2) * u.Y;
update(1, 1, s, prv, -b[u.X], 1);
update(1, 1, s, nxt, u.Y, 1);
update(1, 1, s, prv, -1, 3);
update(1, 1, s, nxt, 1, 3);
b[u.X] = u.Y;
}
cout << ans << "\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset_find(k) find_by_order(k)
#define oset_order(k) order_of_key(k)*/
using ll = long long;
using db = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T> using pqg = priority_queue<T, vector<T>, greater<T> >;
#define FOR(i, a, n) for(int i=(a); i<(n); ++i)
#define ROF(i, a, n) for(int i=(n)-1; i>=(a); --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define ins insert
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string)s; }
string ts(const string& s) { return '"' + s + '"'; }
string ts(bool b) { return (b ? "true" : "false"); }
string ts(vector<bool> v) {
string res = "{";
FOR(i, 0, sz(v))
res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ> string ts(bitset<SZ> b) {
string res;
FOR(i, 0, SZ)
res += char('0' + b[i]);
return res;
}
template <typename A, typename B> string ts(pair<A, B> p);
template <typename A> string ts(A v) {
bool first = 1; string res = "{";
for (const auto& x: v) {
if (!first) res += ", ";
first = 0; res += ts(x);
}
res += "}"; return res;
}
template <typename A, typename B> string ts(pair<A, B> p) {
return "(" + ts(p.fi) + ", " + ts(p.se) + ")";
}
void dbgOut() { cerr << "]" << endl; }
template <typename H, typename... T> void dbgOut(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
dbgOut(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbgOut(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int MOD = 1e9+7; // 998244353;
const ll INF = 1e18;
const int MX = 2e5+5;
vi adj[2005];
bool vis[2005];
int dfs(int i) {
int cnt = 1;
vis[i] = 1;
for (auto x : adj[i]) {
if (!vis[x])
cnt += dfs(x);
}
return cnt;
}
void solve() {
int n, m; cin >> n >> m;
while (m--) {
int x, y; cin >> x >> y;
--x; --y;
adj[x].pb(y);
}
ll ans = 0;
FOR(i, 0, n) {
ans += dfs(i);
fill_n(vis, 2005, 0);
dbg(ans);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
auto begin = std::chrono::high_resolution_clock::now();
#endif
solve();
#ifdef LOCAL
auto end = std::chrono::high_resolution_clock::now();
cerr << "\nExecution time: " << std::chrono::duration_cast<std::chrono::duration<double>> (end-begin).count() << " seconds\n";
#endif
}
| #include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
void input(int *arr, int n){
for(int i=0; i<n; ++i) cin >> arr[i];
}
void input(vector<int>&arr, int n){
for(int i=0; i<n; ++i) cin >> arr[i];
}
void print(int *arr, int n){
for(int i=0; i<n; ++i) cout << arr[i] << " ";
cout << "\n";
}
void print(vector<int>&arr, int n){
for(int i=0; i<n; ++i) cout << arr[i] << " ";
cout << "\n";
}
vector<vector<int> >vec;
vector<bool>visited;
ll ans = 0;
void dfs(int node){
visited[node] = true;
for(int to : vec[node]){
if(visited[to])
continue;
else{
ans += 1;
//cout << ans << " " << node << " " << to << "\n";
dfs(to);
}
}
}
int main(){
FAST;
int test = 1;
//cin >> test;
for(int tt=1; tt<=test; ++tt){
int n, m;
cin >> n >> m;
vec = vector<vector<int> >(n+1, vector<int>());
for(int i=1; i<=m; ++i){
int u, v;
cin >> u >> v;
vec[u].push_back(v);
}
for(int i=1; i<=n; ++i){
visited = vector<bool>(n+1, false);
//cout << i << "\n";
dfs(i);
}
cout << ans+n << "\n";
}
return 0;
} |
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
#define all(s) (s).begin(),(s).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define se second
#define P pair<ll,ll>
const ll mod = 998244353;
//const ll mod = 1000000007;
const ll inf = 2000000000000000000ll;
static const long double pi = 3.141592653589793;
void vcin(vector<ll> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
void vcout(vector<ll> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
void YesNo(bool a){if(a){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}
void YESNO(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
template<class T,class U> void chmax(T& t,const U& u){if(t<u) t=u;}
template<class T,class U> void chmin(T& t,const U& u){if(t>u) t=u;}
ll modPow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
ll binary_gcd(ll u, ll v){
if(min(u, v) == 0) return u|v;
int shift = __builtin_ctzll(u|v);
u >>= __builtin_ctzll(u);
while(v){
v >>= __builtin_ctzll(v);
if(u > v) swap(u, v);
v -= u;
}
return u << shift;
}
ll pov(ll base, ll exp, ll mod){
ll ret = 1;
while(exp){
if(exp&1) ret = (__int128)ret * base % mod;
base = (__int128)base * base % mod;
exp >>= 1;
}
return ret;
}
bool check_composite(ll n, int a, ll d, int s){
ll x = pov(a, d, n);
if(x == 1 || x == n-1) return 0;
for(int r=0;r<s;r++){
x = (__int128)x * x % n;
if(x == n-1) return 0;
}
return 1;
}
bool MillerRabin(ll n, int iter = 5){
if(n < 4) return n == 2 || n == 3;
int s = __builtin_ctzll(n-1);
ll d = (n-1) >> s;
for(int i=0;i<=iter;i++){
int a = rand() % (n-3) + 2;
if(check_composite(n, a, d, s)) return 0;
}
return 1;
}
ll rho_pollard(ll n){
if((n&1) == 0) return 2;
ll x, y, c, g = 1;
x = y = rand() % n + 1;
c = rand() % n + 1;
while(g == 1){
x = ((__int128)x*x+c) % n;
y = ((__int128)y*y+c) % n;
y = ((__int128)y*y+c) % n;
g = binary_gcd(abs(x-y), n);
}
return g;
}
void factorize(ll num, vector<ll> &facts){
if(num == 1) return;
if(MillerRabin(num)){
facts.push_back(num);
return;
}
ll divis = rho_pollard(num);
factorize(divis, facts);
factorize(num/divis, facts);
}
vector<pair<ll,ll>> ml(vector<ll> fact){
vector<pair<ll,ll>> f;
if(int(fact.size())==0){
return f;
}
sor(fact);
ll x=fact[0];
ll y=1;
for(int i=1;i<int(fact.size());i++){
if(x==fact[i]){
y++;
}
else{
pair<ll,ll> z;
z.fi=x;
z.se=y;
f.push_back(z);
x=fact[i];
y=1;
}
}
pair<ll,ll> z;
z.fi=x;
z.se=y;
f.push_back(z);
return f;
}
bool isp(ll a){
bool res=true;
if(a==1){
return false;
}
for(int i=2;i*i<=a;i++){
if(a%i==0){
return false;
}
}
return true;
}
int main() {
/* mod は 1e9+7 */
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout<< fixed << setprecision(20);
ll a,b;
cin>>a>>b;
vector<ll> m(1050000);
m[0]=1;
vector<ll> prime;
map<ll,ll> ip;
vector<ll> check;
for(int i=2;i<=72;i++){
if(isp(i)){
prime.push_back(i);
}
}
check.push_back(0);
for(ll i=a;i<=b;i++){
ll f=0;
for(int j=0;j<20;j++){
if(i%prime[j]==0){
f+=(1<<j);
}
}
for(int j=0;j<int(check.size());j++){
bool t=true;
if((check[j]&f)==0){
if(m[check[j]+f]==0){
check.push_back(check[j]+f);
}
m[check[j]+f]+=m[check[j]];
}
}
}
ll ans=0;
for(int i=0;i<1050000;i++){
ans+=m[i];
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=(1<<20);
bool vis[80];
int prime[25],cnt;
ll dp[N];
void init(){
int maxn=72;
cnt=0;
for(int i=2;i<=maxn;i++){
if(!vis[i])
prime[++cnt]=i;
for(int j=1;j<=cnt&&prime[j]*i<=maxn;j++){
vis[i*prime[j]]=1;
if(i%prime[j]==0)
break;
}
}
}
int main(){
init();
ll a,b;
scanf("%lld%lld",&a,&b);
dp[0]=1;//空集
for(ll i=a;i<=b;i++){
int msk=0;
for(int j=1;j<=cnt;j++){//得到i含有的质因子
if(i%prime[j]==0)
msk|=(1<<(j-1));
}
for(int j=0;j<(1<<cnt);j++){
if(!(msk&j))//表示两个状态互质
dp[msk|j]+=dp[j];
}
}
ll ans=0;
for(int i=0;i<(1<<cnt);i++)
ans+=dp[i];
printf("%lld\n",ans);
return 0;
} |
#include <stdio.h>
#include<algorithm>
#include <math.h>
#include<iostream>
#include <stack>
#include<string.h>
#include<vector>
using namespace std;
int main()
{
long long int n,x;
string a;
cin>>n>>x;
cin>>a;
// scanf("%s",a);
for( int i=0;i<n;i++){
if(a[i]=='o'){
x++;
}
else {
if(x>0){
x--;
}
}
}
cout<<x;
}
| #include<bits/stdc++.h>
/*#include<boost/multiprecision/cpp_int.hpp>
using boost :: multiprecision::cpp_int;*/
#define ll long long int
#define mod 1000007
using namespace std;
/*int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}*/
void solve()
{
char s[3];
char k;
cin >> s;
k = s[0];
for (int i = 0; i < 2; i++)
{
s[i] = s[i + 1];
}
s[2] = k;
for (int i = 0; i < 3; i++)
{
cout << s[i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-5;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N;
cin >> N;
vc<complex<ld>> S(N), T(N);
rep(i, N) {
ld a, b;
cin >> a >> b;
S[i] = {a, b};
}
rep(i, N) {
ld a, b;
cin >> a >> b;
T[i] = {a, b};
}
complex<ld> sc(0, 0), tc(0, 0);
rep(i, N) sc += S[i], tc += T[i];
sc /= N, tc /= N;
rep(i, N){
S[i] -= sc;
T[i] -= tc;
}
rep(i, N) rep(j, N){
vc<complex<ld>> ss(N), tt(N);
rep(si, N) ss[si] = S[si] * polar(1.0L, -arg(S[i]));
rep(ti, N) tt[ti] = T[ti] * polar(1.0L, -arg(T[j]));
bool ok = true;
rep(si, N) {
bool isok = false;
rep(ti, N){
if (abs(ss[si] - tt[ti]) < EPS) isok = true;
}
if(!isok){
ok = false;
break;
}
}
if(ok){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read() {
#define nc getchar()
int x=0;char c=nc;
while(c<48)c=nc;
while(c>47)x=x*10+(c^48),c=nc;
return x;
#undef nc
}
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int maxn=5e5+10;
int n;
const string A="atcoder";
string str;
void solve() {
// scanf("%s",str+1);
// n=strlen(str+1);
cin>>str;
n=str.size();
if(A<str)return puts("0"),void();
int pos=-1,cur=0;
for(char c:str){
if(c!='a'){
pos=cur;break;
}
cur++;
}
if(pos==-1)return puts("-1"),void();
int cnt=0;
while(pos){
swap(str[pos-1],str[pos]),pos--,cnt++;
if(A<str)break;
}
printf("%d\n",cnt);
}
signed main() {
int T=read();
while(T--)solve();
// solve();
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define black_tree tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define pair_black_tree tree<pair<ll,ll>,null_type,less_equal<pair<ll,ll>>,rb_tree_tag,tree_order_statistics_node_update>
#define gp __gnu_pbds
#define INF 1000000000
#define MOD 1000000007
#define MAX 1000001
#define endl '\n'
#define ll long long
#define ld long double
#define lli long long int
#define ull unsigned long long
#define ulli unsigned long long int
#define pb push_back
#define pf push_front
#define ook order_of_key
#define fbo find_by_order
#define ffo find_first_of
#define np next_permutation
#define mp make_pair
#define eb emplace_back
#define mae max_element
#define mie min_element
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
using namespace std;
using namespace gp;
vector<ll>v;
ll n,k,ans,cnt,g[9][9];
string add(string a,string b){
if(a.size()>b.size()) swap(a,b);
string s="";
ll l1=a.size(),l2=b.size(),diff=l2-l1,carry=0;
for(int i=l1-1;i>=0;i--){
ll num=(a[i]-'0'+b[i+diff]-'0')+carry;
s.pb(num%10+'0');
carry=num/10;
}
for(int i=l2-l1-1;i>=0;i--){
ll num=b[i]-'0'+carry;
s.pb(num%10+'0');
carry=num/10;
}
if(carry) s.pb(carry+'0');
reverse(s.begin(),s.end());
return s;
}
string multiply(string a,string b){
if(a.empty() || b.empty()) return "0";
ll l1=a.size(),l2=b.size(),fi=0,fj=0,carry,num1,num2;
vector<ll>result(l1+l2,0LL);
for(int i=l1-1;i>=0;i--){
carry=0,num1=a[i]-'0',fj=0;
for(int j=l2-1;j>=0;j--){
num2=b[j]-'0';
ll sum=num1*num2+result[fi+fj]+carry;
carry=sum/10;
result[fi+fj]=sum%10;
fj++;
}
if(carry) result[fi+fj]+=carry;
fi++;
}
ll i=result.size()-1;
while(i>=0 && !result[i]) i--;
if(i==-1) return "0";
string s="";
while(i>=0) s+=to_string(result[i--]);
return s;
}
int main(){
//freopen("file.in","r",stdin);
//freopen("file.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>g[i][j];
}
}
for(int i=2;i<=n;i++) v.pb(i);
do{
ans=0;
ans+=g[1][v[0]];
for(int i=1;i<v.size();i++) ans+=g[v[i-1]][v[i]];
ans+=g[v[v.size()-1]][1];
if(ans==k) cnt++;
}
while(np(v.begin(),v.end()));
cout<<cnt;
} | #include<bits/stdc++.h>
using namespace std;
#define speed ios_base::sync_with_stdio;cin.tie(nullptr);cin.tie(nullptr);
typedef long long int ll;
#define t int t;cin>>t;while(t--)
#define For(n) for(int i=0;i<n;i++)
#define ne(a) int a;cin>>a;
bool great(pair<int,int>a,pair<int,int>b)
{
return (a.first<b.first);
}
template<class c>
c gcd (c a,c b)
{
if(b==0)
return a;
gcd(b,a%b);
}
int main()
{
speed
ne(n) ne(k)
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j =0;j<n;j++)
{
cin>>arr[i][j];
}
}
int count =0;int result = 0;
int tarr[n];
for(int i =0;i<n;i++)
{
tarr[i] = i;
}
do {
int count =0;
for(int i=0;i<n;i++)
{
count+=arr[tarr[i]][tarr[(i+1)%n]];
}
if(count==k)
result++;
} while (next_permutation(tarr+1, tarr + n));
cout<<result<<endl;
} |
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
#include<tuple>
#include<time.h>
#include<list>
#define DIV 1000000007
using namespace std;
using ll = long long;
using ldb = long double;
vector<int> par;
int root(int a) {
if (par[a] == a)return a;
else return par[a] = root(par[a]);
}
void unite(int a, int b) {
int art = root(a), brt = root(b);
if (art == brt) return;
par[brt] = art;
}
int main() {
int N, M; cin >> N >> M;
par = vector<int>(N + 1);
vector<ll> p(N + 1), q(N + 1);
for (int i = 1; i <= N; i++) {
cin >> p[i];
par[i] = i;
}
for (int i = 1; i <= N; i++) {
cin >> q[i];
}
for (int i = 0; i < M; i++) {
int c, d; cin >> c >> d;
unite(c, d);
}
vector<ll> rtp(N + 1), rtq(N + 1);
for (int i = 1; i <= N; i++) {
int rt = root(i);
//cout << "i=" << i << " rt=" << rt << endl;
rtp[rt] += p[i], rtq[rt] += q[i];
}
bool fl = true;
for (int i = 1; i <= N; i++) {
//cout << rtp[i] << " " << rtq[i] << endl;
if (rtp[i] != rtq[i]) {
fl = false;
break;
}
}
if (fl) cout << "Yes" << endl;
else cout << "No" << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define FILL0(x) memset(x,0,sizeof(x))
#define FILL1(x) memset(x,-1,sizeof(x))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
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; }
vector<ll> G[200000];
bool visited[200000];
vector<vector<ll>> group;
void dfs(int c, ll *ab){
visited[c] = true;
group[group.size()-1].push_back(c);
rep(i, G[c].size()){
if(!visited[G[c][i]]) dfs(G[c][i], ab);
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n, m;
cin >> n >> m;
ll 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--;
G[c].push_back(d);
G[d].push_back(c);
}
ll ab[n];
rep(i, n){
ab[i] = a[i]-b[i];
}
rep(i, n){
if(!visited[i]){
vector<ll> gg;
group.push_back(gg);
dfs(i, ab);
}
}
rep(i, group.size()){
ll tmp = 0;
rep(j, group[i].size()){
tmp += ab[group[i][j]];
}
if(tmp != 0){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(v) (v).size()
#define all(v) (v).begin(),(v).end()
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Time (double)clock()/CLOCKS_PER_SEC
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pi;
template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}
template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}
template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}
const int inf=1000000000,mod=1000000007;
const ll infll=1000000000000000000ll,modll=ll(mod);
const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);
//#define mydef
//#define LOCAL
#ifdef mydef
#define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#endif
#define debug
#define putnum(x) cout<<#x<<": "<<x<<endl
#define putvec(v) cout<<#v<<": ";REP(i,0,sz(v)) cout<<v[i]<<" ";cout<<endl
template <class T> inline void read(T &x){
T w=1;x=0;char c=getchar();
while(c<'0'||c>'9') {if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
x*=w;
}
template <class T> inline void write(T x) {
if(x<0) putchar('-'),x=~x+1;
if(x) write(x/10),putchar(x%10+'0');
}
const int maxn=1005;
const int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
int h,w,ans1,ans2;
int cnt1[maxn],cnt2[maxn];
int a[maxn][maxn],vis[maxn][maxn][4],hav[maxn][maxn];
string str;
struct point {
int x,y;
int dir;
};
queue<point> q;
bool cango(point pos) {
if(pos.x<1||pos.y<1||pos.x>h||pos.y>w) return false;
return true;
}
vector<int> v1[maxn],v2[maxn];
void dfs1(int pos) {
cnt1[pos]=0;
for(int i=0;i<sz(v1[pos]);i++) {
int to=v1[pos][i];
if(!cnt1[to]) continue;
dfs1(to);
}
return;
}
void dfs2(int pos) {
cnt2[pos]=0;
for(int i=0;i<sz(v2[pos]);i++) {
int to=v2[pos][i];
if(!cnt2[to]) continue;
dfs2(to);
}
return;
}
int main() {
#ifdef LOCAL
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
scanf("%d%d",&h,&w);
for(int i=1;i<=h;i++) {
cin>>str;
for(int j=1;j<=w;j++) {
if(str[j-1]=='#')
a[i][j]=1;
}
}
for(int i=0;i<4;i++) {
q.push((point){1,1,i});
vis[1][1][i]=1;
}
while(!q.empty()) {
point pos=q.front();
hav[pos.x][pos.y]=1;
q.pop();
point to=pos;
to.x+=dx[to.dir];
to.y+=dy[to.dir];
if(!cango(to)) continue;
point ag=to;
ag.x+=dx[to.dir];
ag.y+=dy[to.dir];
if(!cango(ag)||a[to.x][to.y]==1) {
for(int i=0;i<4;i++) {
if(!vis[to.x][to.y][i]) {
q.push((point){to.x,to.y,i});
vis[to.x][to.y][i]=1;
}
}
} else {
if(!vis[to.x][to.y][to.dir]) {
q.push(to);
vis[to.x][to.y][to.dir]=1;
}
}
}
/* for(int i=1;i<=h;i++) {
for(int j=1;j<=w;j++) {
cout<<hav[i][j]<<" ";
}
cout<<endl;
}*/
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
if(!hav[i][j])
cnt1[i]=1,cnt2[j]=1;
for(int i=1;i<=h;i++) {
int is=0;
for(int j=1;j<=w;j++) {
if(hav[i][j]) continue;
if(is&&a[i][j]) {
v2[j].pb(is);
v2[is].pb(j);
}
if(a[i][j]) is=j;
}
}
for(int i=1;i<=w;i++) {
int is=0;
for(int j=1;j<=h;j++) {
if(hav[j][i]) continue;
if(is&&a[j][i]) {
v1[j].pb(is);
v1[is].pb(j);
}
if(a[j][i]) is=j;
}
}
for(int i=1;i<=h;i++) {
if(cnt1[i]) {
ans1++;
dfs1(i);
}
}
for(int i=1;i<=w;i++) {
if(cnt2[i]) {
ans2++;
dfs2(i);
}
}
cout<<min(ans1,ans2)<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
const ll N=1010;
char c[1010][1010];
char d[1010][1010];
/*---------------- Union Find (DSU) ------------------*/
int pa[N],sz[N];
ll find_pa(ll pos){
if(pa[pos]!=pos)pa[pos]=find_pa(pa[pos]);
return pa[pos];
}
void join(int a, int b) {
int x=find_pa(a),y=find_pa(b);
if (x!=y) {
if(sz[x]>=sz[y]) sz[x]+=sz[y],pa[y]=x;
else sz[y]+=sz[x],pa[x]=y;
}
}
void dsu_init(ll n){fore(i,0,n)pa[i]=i,sz[i]=1;}
int main(){FIN;
ll n,m; cin>>n>>m;
fore(i,0,n){
fore(j,0,m){
cin>>c[i][j];
}
}
c[0][0]='#';
c[0][m-1]='#';
c[n-1][0]='#';
c[n-1][m-1]='#';
dsu_init(n);
fore(j,0,m){
vector<ll> v;
fore(i,0,n){
if(c[i][j]=='#')v.pb(i);
}
fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
}
ll res0=0;
fore(i,1,n){
if(find_pa(0)!=find_pa(i)){
res0++;
join(0,i);
}
}
fore(i,0,n){
fore(j,0,m){
d[j][i]=c[i][j];
}
}
swap(n,m);
fore(i,0,n){
fore(j,0,m){
c[i][j]=d[i][j];
}
}
dsu_init(n);
fore(j,0,m){
vector<ll> v;
fore(i,0,n){
if(c[i][j]=='#')v.pb(i);
}
fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
}
ll res1=0;
fore(i,1,n){
if(find_pa(0)!=find_pa(i)){
res1++;
join(0,i);
}
}
cout<<min(res0,res1)<<"\n";
return 0;
} |
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll l,dp[N][14];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>l;
dp[0][0]=1;
REP1(i,l){
REP1(k,12){
REP(j,i){
dp[i][k]+=dp[j][k-1];
}
}
}
cout<<dp[l][12]<<"\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int L;
cin >> L;
vector<vector<long>> ncr;
auto pas = [&](auto n) {
if (n < (int)ncr.size()) return;
ncr.resize(n + 1, vector<long>(n + 1));
ncr.at(0).at(0) = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= i; j++) {
ncr.at(i).at(j) += ncr.at(i - 1).at(j);
if (j) ncr.at(i).at(j) += ncr.at(i - 1).at(j - 1);
}
}
};
pas(1000);
cout << ncr.at(L - 12 + 11).at(11) << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int INF = 2e9;
const long long inf = 1LL<<62;
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<998244353> mint;
int main() {
int n;
cin >> n;
vector<int> a(n);
mint ans = 0;
for (int i=0; i<n; i++) {
cin >> a[i];
ans += mint(a[i])*mint(a[i]);
}
sort(a.begin(), a.end());
vector<mint> acc(n+1, 0);
for (int i=0; i<n; i++) {
acc[i+1] = mint(2)*acc[i] + mint(a[i]);
}
for (int i=1; i<n; i++) {
ans += mint(a[i])*acc[i];
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll ;
typedef pair <int, int > pii ;
typedef vector <int> vi ;
#define umap unordered_map
// #define endl '\n'
#define vec vector
#define all(v) (v).begin() , (v).end()
#define sumof(v) accumulate(all(v), 0LL, plus<ll>())
#define maxof(v) max_element(all(v))
#define minof(v) min_element(all(v))
#define rev(v) reverse(all(v))
#define sz(v) (long long)(v).size()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define PI acos(-1)
#define tc(t) ll testcases ; cin >> testcases ; for(int testcase = 1 ; testcase <= testcases ; testcase++)
#define printstl(c) for (auto it = (c).begin(); it != --((c).end()); it++) cout << *it << " "; cout << *(c.rbegin()) << "\n" << flush
#define printv(v, s, e) for(int i=(s); i<(e); i++) cout<<(v)[i]<<" "; cout<<endl
#define printnl(v) cout << v << "\n" << flush
#define invec(vec_name, vec_size) vec<ll> vec_name(vec_size); for(int i=0; i<vec_size; i++) cin >> vec_name[i];
#define fastio ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__);
#else
#define pr(...) {} ;
#endif
#define Printer os << "[ "; for (auto &it : p) os << it << " "; return os << "]";
#define PairPrint return os << "(" << p.first << ", " << p.second << ")";
#define printCase(n) cout<<"Case #"<<(n)<<":"<<" "
template <class T> ostream &operator <<(ostream &os, const vector<T> &p) {Printer}
template <class T> ostream &operator <<(ostream &os, const unordered_set<T> &p) {Printer}
template <class T> ostream &operator <<(ostream &os, const set<T> &p){Printer}
template <class T> ostream &operator <<(ostream &os, const multiset<T> &p){Printer}
template <class S, class T> ostream &operator <<(ostream &os, const pair<S, T> &p) {PairPrint}
template <class S, class T> ostream &operator <<(ostream &os, const unordered_map<S, T> &p){Printer}
template <class S, class T> ostream &operator <<(ostream &os, const map<S, T> &p){Printer}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n"; }
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...); }
ll modExp(ll x, ll y, ll mod) {x %= mod, y %= (mod - 1) ; ll res = 1; while (y) {if (y & 1) res = (res * x) % mod; y /= 2, x = (x * x) % mod; } return res % mod; }
int cel(int num, int den) {if(num % den == 0) return num / den; else return 1 + num / den; }
// vi r = {0,1,1,0};
// vi c = {0,1,0,1};
// vector<int> getRand(int x, int val)
// {
// srand(time(NULL));
// vi random;
// for(int i=0; i<x; i++) random.pb(rand()%val);
// return random;
// }
vector<int> constructPrefix(vector<int> v, int mod)
{
int n = v.size();
vector<int> ans;
int sum = 0;
for(int i = 0; i < n; i++)
{
sum = (sum%mod + v[i]%mod)%mod;
ans.push_back(sum);
}
return ans;
}
// void seive()
// {
// for(int i = 2; i <= LIMIT; i++)
// {
// if(not done[i])
// {
// primes.pb(i);
// for(int j = i; j <= LIMIT; j += i)
// {
// done[j] = 1;
// }
// }
// }
// }
// void getFreq(vi &v, map<int, int> &m)
// {
// for(int i = 0; i < v.size(); i++)
// {
// m[v[i]]++;
// }
// }
int32_t main()
{
fastio
#ifndef ONLINE_JUDGE
freopen("./input.txt", "r", stdin);
#endif
int n;
cin>>n;
invec(v,n);
sort(all(v));
vi mn(n,0), dp(n,0);
mn[0]= v[0];
int mod = 998244353;
dp[0]= (v[0]*v[0])%mod;
for(int i=1; i<n; i++)
{
v[i] = v[i]%mod;
mn[i] = (mn[i-1]*2 +v[i])%mod;
dp[i]= (dp[i-1]%mod + (((mn[i-1] + v[i])%mod)*v[i])%mod)%mod;
}
// pr(dp)
cout<<dp[n-1]<<endl;
} |
#include<iostream>
#include<cstring>
#define debug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef long long ll;
using PII = pair<int,int>;
const int N = 2e5 + 10 , inf = 1e9;
int he[N << 1] ,nex[N << 1] , v[N << 1] , idex;
void add(int a,int b){
nex[idex] = he[a];
v[idex] = b;
he[a] = idex ++;
}
int n , k;
int f[N] , g[N];
int mid;
int cnt;
void dfs(int fa,int node){
f[node] = inf , g[node] = 0;
for(int i = he[node] ; ~i; i = nex[i]){
if(v[i] == fa) continue;
dfs(node , v[i]);
f[node] = min(f[node] , f[v[i]] + 1);
g[node] = max(g[node] , g[v[i]] + 1);
}
if(f[node] + g[node] <= mid) g[node] = -inf;
else if(g[node] == mid) g[node] = -inf, f[node] = 0 , ++cnt;
}
int main(){
memset(he,0xff,sizeof he);
cin >> n >> k;
for(int i = 1 , a , b ; i < n ; i ++){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
int l = 0 , r = n ;
while(l < r){
mid = (l + r) >> 1;
cnt = 0;
dfs( -1 ,1);
if(g[1] >= 0) cnt ++ ;
if(cnt > k) l = mid + 1;
else r = mid;
}
cout << l << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ALL(v) v.begin(), v.end()
#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 FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define dbg(x) cout << (#x) << " is " << (x) << endl;
#define dbg2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << y << endl;
#define dbgarr(x, sz) \
for (int asdf = 0; asdf < (sz); asdf++) cout << x[asdf] << ' '; \
cout << endl;
#define dbgarr2(x, rose, colin) \
for (int asdf2 = 0; asdf2 < rose; asdf2++) { \
dbgarr(x[asdf2], colin); \
}
#define dbgitem(x) \
for (auto asdf = x.begin(); asdf != x.end(); asdf++) cout << *asdf << ' '; \
cout << endl;
const int inf = -1e8, MAXN = 2e5;
int n, K, f[MAXN], g[MAXN];
vi adj[MAXN];
struct Solution {
int t;
int solve() {
int low = 0, high = n; // time needed to spread info
while (low <= high) {
t = (low + high) / 2;
if (able())
high = t - 1;
else
low = t + 1;
}
return low;
}
bool able() {
int res = 0;
dfs(0, -1);
REP(i, n) {
res += f[i] == t;
}
return res + (g[0] >= 0) <= K;
}
void dfs(int i, int p) {
f[i] = -1, g[i] = 0;
for (int j : adj[i]) {
if (j == p) continue;
dfs(j, i), f[i] = max(f[i], f[j] - 1), g[i] = max(g[i], g[j] + 1);
}
if (f[i] >= g[i])
g[i] = -1; // every node is covered
else if (g[i] == t) // make this a source
g[i] = -1, f[i] = t;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> K;
int a, b;
REP(i, n - 1) {
cin >> a >> b, adj[--a].push_back(--b), adj[b].push_back(a);
}
Solution test;
cout << test.solve() << endl;
} |
// UTF−8
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using lc = complex<ll>;
/* #include<atcoder/all> */
/* using namespace atcoder; */
template<class T>bool chmax(T &a, const T &b) { return (a<b ? (a=b,1) : 0); }
template<class T>bool chmin(T &a, const T &b) { return (a>b ? (a=b,1) : 0); }
int main(void) {
constexpr ll MOD = 1 ? 1e9+7 : 998244353;
const double PI = acos(-1);
constexpr double eps = 1e-10;
cout << fixed << setprecision(32);
cin.tie(0); ios::sync_with_stdio(false);
auto f = [&](char x) {
if('0' <= x && x <= '9')
return x - '0';
return x - 'A' + 10;
};
if(1) {
string n;
ll k;
cin >> n >> k;
ll s = n.size();
vector<vector<ll>> dq(s, vector<ll>(16+1)); // dp ぴったし, dq 未満
dq[0][0] = 1;
dq[0][1] = n[0] == '1' ? 0 : (f(n[0]) - 1);
set<ll> v;
v.insert(f(n[0]));
for(ll i=1; i<s; i++) {
(dq[i][0] += (dq[i-1][0])) %= MOD;
for(ll j=0; j<=16; j++) {
(dq[i][j] += (j * dq[i-1][j])) %= MOD;
if(j == 0)
(dq[i][j+1] += ((15-j) * dq[i-1][j])) %= MOD;
else if(j < 16)
(dq[i][j+1] += ((16-j) * dq[i-1][j])) %= MOD;
}
for(ll j=0; j<f(n[i]); j++) {
(dq[i][v.size() + !v.count(j)] += 1) %= MOD;
}
v.insert(f(n[i]));
}
ll r = dq[s-1][k] + (v.size() == k);
cout << (r % MOD) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define N 60
#define INF 1000000005
#define INFLL (ll)1000000000000000005
#define PI 3.1415926535897
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) int(x.size())
#define IN(a) cin>>(a)
#define OUT(a) cout<<(a)<<endl
#define per(a,mod) ((a)%(mod)+(mod))%(mod)
typedef pair<int,int> P;
const int MAX = (1<<20);
const int MOD = 998244353;
int par[MAX];
int depth[MAX];
int siz[MAX];
//初期化
void init(){
for(int i=0;i<MAX;i++){
par[i]=i;
siz[i]=1;
}
}
//根を探索
int root(int x){
if(par[x]==x){
return x;
}else{
return par[x]=root(par[x]);
}
}
//統合
void unit(int x,int y){
x=root(x);
y=root(y);
if(x==y)return;
if(depth[x]>depth[y])swap(x,y);
par[x]=y;
siz[y]+=siz[x];
if(depth[x]==depth[y])depth[x]++;
}
// 同じ集合か判定
bool issame(int x,int y){
return root(x)==root(y);
}
//集合の大きさ
int size(int x){
return siz[root(x)];
}
bool ext[MAX];
int modPow(long long a, long long n) {
if(n==0)return 1;
if (n == 1) return a % MOD;
if (n % 2 == 1) return (a * modPow(a, n - 1)) % MOD;
long long t = modPow(a, n / 2);
return (t * t) % MOD;
}
void solve(){
int n;
cin>>n;
init();
REP(i,n){
int f;
cin>>f;
f--;
unit(i,f);
ext[f]=1;
}
map<int,int>mp;
REP(i,n){
if(!ext[i])continue;
mp[root(i)]++;
}
int ans=modPow(2,SZ(mp))-1;
if(ans<0)ans+=MOD;
OUT(ans);
}
int main(){
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<string> st;
void solve1()
{
vector<string> print;
string s;
int cnt = 0;
for (auto iter = st.begin(); iter != st.end(); iter++) {
s += *iter;
if (cnt == 20) break;
if (s.length() > 18) {
while (s.length() > 20) {
s.pop_back();
}
print.push_back(s);
cnt++;
s = "";
}
}
for (auto iter = print.begin(); iter != print.end(); iter++) {
cout << *iter;
for (int i = 0; i < 20 - (*iter).length(); i++) {
cout << ".";
}
cout << endl;
}
}
void solve2()
{
string print[20];
int h = 0;
for (auto iter = st.begin(); iter != st.end(); iter++) {
print[h % 20] += *iter;
h++;
}
for (int h = 0; h < 20; h++) {
for (int w = 0; w < 20; w++) {
cout << print[h][w];
}
cout << endl;
}
}
bool seen[20][20];
string M[20];
int dy[4] = {0,1,0,-1};
int dx[4] = {1,0,-1,0};
bool comp(const std::string& lh, const std::string& rh) {
return lh.length() < rh.length();
}
class Comp {
public:
bool operator() (const std::string& lh, const std::string& rh) {
return lh.length() < rh.length();
}
};
int main()
{
int n,m;
cin >> n >> m;
vector<string> s;
for (int i = 0; i < m; i++) {
string t;
cin >> t;
//s.push_back(t);
st.emplace(t);
}
solve1();
//solve2();
} | #include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) ((x)&(-x))
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
inline ll readll()
{
ll x=0;int f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
ll f[110],n;
int m;
vi ans,a;
int main()
{
f[0]=1;f[1]=1;m=88;
rep(i,2,m) f[i]=f[i-2]+f[i-1];
//rep(i,1,10) cout << f[i] << " ";cout << endl;
n=readll();
per(i,m,1)
{
if (n>=f[i]) {n-=f[i];a.pb(i);}
}
int siz=a.size();
int len=(a[0]+1)/2,pos=siz-1;
//rep(i,0,siz-1) printf("%d ",a[i]);puts("");
rep(i,1,len)
{
ans.pb(3);ans.pb(4);
while ((pos>=0) && ((a[pos]+1)/2==i))
{
if (a[pos]&1) ans.pb(2);else ans.pb(1);pos--;
}
}
len=ans.size();
printf("%d\n",len);
per(i,len-1,0) printf("%d\n",ans[i]);
return 0;
}
|
//Bismillah Hir Rehmanir Rahim
//Allah knows best
#include<bits/stdc++.h>
using namespace std;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#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>
#define endl "\n"
#define mini -10000000000000000
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f(i, a, n) for( i=a; i<n;i++)
#define ll long long
#define pi 3.141592653589793
#define vi vector<int>
#define si set<int>
#define pb push_back
#define mp make_pair
#define mod 1000000000000000000
#define rr return 0
#define ff first
#define in insert
#define ss second
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
#define setbits(x) builtin_popcountll(x)
#define zrbits(x) builtin_ctzll(x)
#define deb(x) cout << #x << " = " << x << endl
#define set_bit(x, idx) x = x|1LL<<idx
#define check_bit(x, idx) min(x&(1LL<<idx),1LL)
#define toggle_bit(x, idx) x = x^(1LL<<idx)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> rordered_set;
#ifdef DEBUG
#define ok cerr << __LINE__ <<"is done "<<endl;
#else
#define ok
#endif
ll ncr[1001][1001];
void calcncr()
{
for(int i = 0; i <= 1000; i++)
{
ncr[i][0] = 1;
}
for(int i = 1; i <= 1000; i++)
{
for(int j = 1; j <= 1000; j++)
{
ncr[i][j] = (ncr[i-1][j-1] + ncr[i-1][j])%mod;
}
}
}
int main()
{
ios;
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ll i,j,n,m,k;
ll t;
t=1;
calcncr();
//cin>>t;
while(t--)
{
ll l;
cin>>l;
cout<<ncr[l-1][l-12]<<endl;
}
rr;
} | #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
struct UnionFind{
vl p;
vl rank;
vl cnt;
UnionFind(ll n){
rank.resize(n,0);
p.resize(n,0);
cnt.resize(n,0);
rep(i,n){
p[i] = i;
rank[i] = 0;
cnt[i] = 1;
}
}
ll find(ll x){
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
bool same(ll x, ll y){
return find(x) == find(y);
}
void unite(ll x, ll y){
x = find(x);
y = find(y);
if(x == y) return;
if(rank[x] > rank[y]){
p[y] = x;
cnt[x] += cnt[y];
}else{
p[x] = y;
cnt[y] += cnt[x];
if(rank[x] == rank[y]) rank[y]++;
}
}
ll size(ll x){
return cnt[find(x)];
}
};
UnionFind uf(202020);
int main(){
int n; cin >> n;
vl a(n); rep(i,n) cin >> a[i];
int ans = 0;
rep(i,n/2){
if(uf.same(a[i],a[n-i-1])) continue;
uf.unite(a[i],a[n-i-1]);
ans++;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define inf 1000000000000000001
#define mod 1000000007
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL);
#define precise fixed(cout);cout<<setprecision(16);
#define Set(N,p) N=((N)|((1LL)<<(p)))
#define Reset(N,p) N=((N)&(~((1LL)<<(p))))
#define Check(N,p) (!(((N)&((1LL)<<(p)))==(0)))
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
#define OUT(x) for(auto a:x) cout << a << " "; cout << endl;
#define OK cout << "@===================ok===================@" <<endl;
#define WTF cout <<"< "<<lo<<" | "<< hi <<" >" << endl;
using namespace std;
int main()
{
ll n,x;
cin >> n >> x;
string s;
cin >> s;
ll res=x;
for(auto a:s){
if(a=='x') res--;
else res++;
if(res<0) res=0;
}
cout << res <<"\n";
}
| #include "bits/stdc++.h"
using namespace std;
#define all(x) (xi).begin(), (x).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define un_map unordered_map
#define rep(i,a,n) for (int i = a; i < n; ++i)
#define rrep(i,a,n) for (int i = a; i >= n; --i)
#define irep(i,a,n,inc) for (int i = a; i < n; i += inc)
#define endl "\n"
typedef long long ll;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
int power_mod(int a, int b, int mo) {
int ans = 1;
while (b) {
if(b & 1) {
ans = (ans % mo * a % mo) % mo;
}
a = (a % mo * a % mo) % mo;
b /= 2;
}
return ans;
}
void print(vector<vector<int>> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
cout << i << " : ";
for (int j = 0; j < (int)v[i].size(); ++j) {
cout << v[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int32_t main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
vector<int> m(4e5);
vector<vector<int>> ans(4e5);
for (int i = 0; i < n; ++i) {
ans[m[v[i]]].push_back(v[i]);
m[v[i]]++;
}
for (int i = 0; i < 4e5; ++i) {
sort(ans[i].begin(), ans[i].end());
}
//int maxm = *max_element(all(v));
int sum = 0;
vector<int> res;
for (int i = 0; i < 4e5; ++i) {
int size = (int)ans[i].size();
//if (size) {
//debug(i, ans[i]);
int p = -1;
//int val = ans[i][size - 1];
for (int j = 0; j < size; ++j) {
if (ans[i][j] == p + 1) p++;
else break;
}
//debug(p, val);
//if (p == val + 1) {
res.push_back(p + 1);
// }
// }
}
sort(res.rbegin(), res.rend());
for (int i = 0; i < k; ++i) {
sum += res[i];
}
cout << sum;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n; cin>> n;
cout << n - 1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl
using lint = long long;
typedef pair<int,int> P;
const bool debugFlag=true;
const lint linf=1e18+7;
const lint inf=1e9+7;
const int MOD=1000000007;
signed main(){
int n;cin>>n;
cout<<max(0,n-1)<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
int main(void){
// Your code here!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,ans,m,n,s;
ans=INT_MAX;
cin >> n >> m;
ll a[n],c[n];
vector<ll> v(n),b(m);
for(i=0;i<n;i++)
cin >> v[i];
for(i=0;i<m;i++)
cin >> b[i];
if(n==1)
{
for(i=0;i<m;i++)
ans=min(ans,abs(b[i]-v[0]));
cout << ans;
return 0;
}
sort(v.begin(),v.end());
for(i=1;i<n;i++)
{
a[i]=v[i]-v[i-1];
c[i-1]=v[i]-v[i-1];
}
for(i=2;i<n;i++)
{
a[i]+=a[i-2];
c[n-i-1]+=c[n-i+1];
}
for(i=0;i<m;i++)
{
auto it=lower_bound(v.begin(),v.end(),b[i]);
s=0;
if(b[i]>=v[n-1])
s=a[n-2]+b[i]-v[n-1];
else if(b[i]<=v[0])
s=c[1]+v[0]-b[i];
else if((it-v.begin())%2)
{
s+=c[it-v.begin()];
s+=b[i]-v[it-v.begin()-1];
if((it-v.begin())!=1)
s+=a[it-v.begin()-2];
}
else
{
if(it!=v.begin())
s+=a[it-v.begin()-1];
s+=v[it-v.begin()]-b[i];
if((it-v.begin())!=(n-1))
s+=c[it-v.begin()+1];
}
ans=min(ans,s);
//cout << s << " ";
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
void chmin(ll& a, ll b) {
if (a > b) a = b;
}
int main() {
int N, M;
cin >> N >> M;
ll H[N];
rep(i, N) {
cin >> H[i];
}
sort(H, H+N);
vector<ll> left(N/2), right(N/2);
rep(i, N/2) {
left[i] = H[2*i+1] - H[2*i];
right[i] = H[2*(i+1)] - H[2*i+1];
}
vector<ll> left_sum(N/2 + 1, 0), right_sum(N/2+1, 0);
rep(i, N/2) {
left_sum[i+1] = left_sum[i] + left[i];
right_sum[i+1] = right_sum[i] + right[i];
}
vector<ll> W(M);
rep(i, M) {
cin >> W[i];
}
ll ans = 10000000000000000;
rep(i, M) {
// 先生を初めて超えるindex
int x = lower_bound(H, H +N, W[i]) - H;
if (x % 2) {
chmin(ans, left_sum[(x-1)/2] + (W[i]-H[x-1])
+ right_sum[(N-1)/2] - right_sum[(x-1)/2]);
} else {
chmin(ans, left_sum[x/2] + (H[x]-W[i])
+ right_sum[(N-1)/2] - right_sum[x/2]);
}
}
cout << ans << endl;
} |
#include<stdio.h>
#include<algorithm>
#include<random>
#include<queue>
#include<map>
#include<time.h>
#include<string>
using namespace std;
using lld = long long int;
using pii = pair<int, int>;
using pll = pair<lld, lld>;
using pli = pair<lld, int>;
using pil = pair<int, lld>;
using lf = double;
int n, m;
int lv[200009];
int dv[200009];
int a[200009];
int b[200009];
int al, bl;
#define I 262144
lld ms[2 * I];
lld getms(int dep, int ql, int qr, int ll, int rr) {
if (ql <= ll && rr <= qr)return ms[dep];
if (qr < ll || rr < ql)return 0;
return getms(dep * 2, ql, qr, ll, (ll + rr) / 2) + getms(dep * 2 + 1, ql, qr, (ll + rr) / 2 + 1, rr);
}
void updatems(int i, int k) {
i += I;
while (i > 0) {
ms[i]+=k;
i >>= 1;
}
}
vector<int> rv[200009];
int main()
{
int i, j, k, l;
int t = 1, tv = 0;
while (t--) {
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
lv[i] = m;
}
for (i = 0; i < m; i++) {
dv[i] = n;
}
int p;
scanf("%d", &p);
while (p--) {
scanf("%d %d", &i, &j);
i--; j--;
lv[i] = min(lv[i], j);
dv[j] = min(dv[j], i);
}
lld res = 0;
al = dv[0];
for (i = 0; i < al; i++) {
a[i] = lv[i];
res += a[i];
updatems(i, 1);
rv[a[i]].push_back(i);
}
bl = lv[0];
for (i = 0; i < bl; i++) {
b[i] = dv[i];
res += b[i];
res -= getms(1, 0, b[i] - 1, 0, I - 1);
for (auto it : rv[i]) {
updatems(it, -1);
}
}
printf("%lld\n", res);
}
} | // Created by ash_98
#include<bits/stdc++.h>
using namespace std;
#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
~printer(){cerr<<endl;}
Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
Rics(string x){cerr<<x;return *this;}
Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
Gene c >Rics(rge<c> x){
*this,"["; for(auto it = x.b; it != x.e; ++it)
*this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) {
return uniform_int_distribution<int>(l, r) (rng);
}
#define mx 200005
#define ll long long
#define mod 1000000007
char ch[mx];
int n,m,ii,k;
int row[mx],col[mx];
namespace Segment_Tree
{
const int N=200005;
vector<int>Tree[N*4];
int Lazy[mx*4];
int ar[N];
void Relax(int node,int be,int en)
{
if(!Lazy[node])return;
// Tree[node]+=Lazy[node];
if(be!=en)
{
Lazy[node*2]+=Lazy[node];
Lazy[node*2+1]+=Lazy[node];
}
Lazy[node]=0;
}
void init(int node,int be,int en)
{
//Lazy[node]=0;
if(be==en)
{
Tree[node].push_back(ar[be]);
return;
}
int mid=(be+en)/2;
init(node*2,be,mid);
init(node*2+1,mid+1,en);
merge(Tree[node*2].begin(),Tree[node*2].end(),Tree[node*2+1].begin(),Tree[node*2+1].end(),back_inserter(Tree[node]));
}
int query(int node,int be,int en,int i,int j,int val)
{
//Relax(node,be,en);
if(be>j || en<i)return 0;
if(be>=i && en<=j)
{
int st=0,fi=Tree[node].size()-1;
int sz=Tree[node].size();
int re=sz;
while(st<=fi)
{
int mid=(st+fi)/2;
if(Tree[node][mid]>=val)
{
re=mid;
fi=mid-1;
}
else st=mid+1;
}
return sz-re;
}
int mid=(be+en)/2;
return (query(node*2,be,mid,i,j,val)+query(node*2+1,mid+1,en,i,j,val));
}
void dbg_test(int node,int be,int en)
{
if(be==en)return;
int mid=(be+en)/2;
dbg_test(node*2,be,mid);
dbg_test(node*2+1,mid+1,en);
}
}
using namespace Segment_Tree;
void solve()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)row[i]=m;
for(int i=1;i<=m;i++)col[i]=n;
for(int i=1;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
col[y]=min(col[y],x-1);
row[x]=min(row[x],y-1);
}
ll re=0;
int limi=row[1];
re+=limi-1;
for(int i=2;i<=limi;i++)
{
int jabe=col[i];
re+=jabe-1;
ar[i]=jabe;
//cout<<i<<" "<<jabe<<endl;
}
init(1,1,m);
limi=col[1];
re+=limi;
//cout<<re<<endl;
for(int i=2;i<=limi;i++)
{
int r=row[i];
int jabe=query(1,1,m,2,r,i);
//cout<<i<<" "<<r-1<<" "<<jabe<<endl;
re-=jabe;
re+=r-1;
}
printf("%lld\n",re);
}
int main()
{
int t=1;
//scanf("%d",&t);
while(t--)solve();
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.