code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
int32_t main()
{
int t;
//cin>>t;
t=1;
while(t--)
{
int n;cin>>n;
map<string,int> one;
map<string,int> two;
while(n--){
string s;
cin>>s;
if(s[0]=='!'){
int size=s.size();
s=s.substr(1,size-1);
two[s]++;
}
else{
one[s]++;
}
}
bool found=false;
string ans="satisfiable";
for(auto c:one){
if(two.find(c.first)!=two.end()){
found=true;
ans=c.first;
}
}
if(!found)
cout<<"satisfiable"<<endl;
else
cout<<ans<<endl;
}
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
int T;
int extGCD(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
int ret = extGCD(b, a % b, y, x);
y -= a / b * x;
return ret;
}
int unit(int a, int b)
{
if(a < 0)return 0;
return (a + b - 1) / b;
}
signed main()
{
cin >> T;
rep(_, T)
{
int X, Y, P, Q;
cin >> X >> Y >> P >> Q;
int T1 = 2 * (X + Y), T2 = P + Q;
int ans = -1;
rep(i, Y)rep(j, Q)
{
int x, y;
int c = P + j - X - i;
int d = extGCD(T1, -T2, x, y);
if(d < 0)
{
d = -d;
x = -x;
y = -y;
}
if(c % d != 0)continue;
// cout << T1 << "*" << x << "+" << -T2 << "*" << y << "=" << d << endl;
x *= c / d, y *= c / d;
// cout << T1 << "*" << x << "+" << -T2 << "*" << y << "=" << c << endl;
int t1 = T1 / d, t2 = T2 / d;
if(x < 0 || y < 0)
{
int MAX = max(unit(-x, t2), unit(-y, t1));
// cout << "((" << -x << ", " << t2 << ")" << unit(-x, t2) << ", " << "(" << -y << ", " << t2 << ")" << unit(-y, t1) << ")" << endl;
// cout << "MAX : " << MAX << endl;
x += MAX * t2, y += MAX * t1;
if(ans == -1)ans = x * T1 + i + X;
else ans = min(ans, x * T1 + i + X);
// assert(x * T1 + i + X == y * T2 + P + j);
}
else
{
int MIN = min(x / t2, y / t1);
// cout << "MIN : " << MIN << endl;
x -= MIN * t2, y -= MIN * t1;
if(ans == -1)ans = x * T1 + i + X;
else ans = min(ans, x * T1 + i + X);
// assert(x * T1 + i + X == y * T2 + P + j);
}
}
if(ans == -1)cout << "infinity " << endl;
else cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define ve vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vp vector<pair<ll, ll>>
#define all(v) (v).begin(), (v).end()
#define sortve(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define f1(i, n) for (ll i = 1; i < n + 1; i++)
#define f2(i, a, b) for (ll i = a; i < b; i++)
#define precision(x) cout << fixed << setprecision(x);
#define testcase \
ll t; \
cin >> t; \
while (t--)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(nullptr); \
cout.tie(nullptr)
#define bas cout << "\n";
const ll PI = 3.1415926535897932384626;
const ll mod = 1000000007;
const ll FMOD = 998244353;
const ll N = 100005;
const double eps = 1e-9;
// Let's Go 😇
int main()
{
double a, b, c, d;
cin >> a >> b >> c >> d;
if (a > c)
{
swap(a, c);
swap(b, d);
}
//cout << a << b << c << d;
double tan = (b + d) / (c - a);
// cout << tan;
double ans = a + (b / tan);
precision(6);
cout << ans;
}
/* SERIOUSLY, It ⬇ ain't a JOKE ⬇ */
/* The problem is either
in conditional statements
or the increments or %mod or
endl or you are printing
something else*/ | // ABC196C
// 幾何
// 各色ごとの最終位置は二択.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N;
cin >> N;
map<int, vector<int>> m;
for (int i = 0; i < N; i++) {
int x, c;
cin >> x >> c;
m[c].push_back(x);
}
vector<vector<int>> pos(N + 1, vector<int>(2, 0));
vector<vector<int>> sec(N + 1, vector<int>(2, INF));
sec[0] = {0, 0};
int i = 0;
for (auto& [_, v] : m ) {
auto max = *max_element(v.begin(), v.end());
auto min = *min_element(v.begin(), v.end());
for (int j = 0; j < 2; j++) {
// max first
int tmp = abs(max - pos[i][j]) + max - min + sec[i][j];
if (tmp < sec[i + 1][0]) {
sec[i + 1][0] = tmp;
pos[i + 1][0] = min;
}
int tmp2 = abs(pos[i][j] - min) + max - min + sec[i][j];
if (tmp2 < sec[i + 1][1]) {
sec[i + 1][1] = tmp2;
pos[i + 1][1] = max;
}
}
++i;
}
cout << min(sec[i][0] + abs(pos[i][0]), sec[i][1] + abs(pos[i][1])) << endl;
return 0;
}
|
#include<bits/stdc++.h>
int main(){
using namespace std;
const auto scan{[]{unsigned long r; cin >> r; return r;}};
const auto N{scan()}, M{scan()};
const auto [h, dif]{[&N]{
vector<unsigned long> h(N);
for(auto&& i : h)cin >> i;
sort(begin(h), end(h));
vector<unsigned long> rp{0}, ra;
for(unsigned long i{0}; i + 1 < N; i += 2)rp.push_back(h[i + 1] - h[i]);
for(unsigned long i{1}; i + 1 < N; i += 2)ra.push_back(h[i + 1] - h[i]);
ra.push_back(0);
partial_sum(begin(rp), end(rp), begin(rp));
partial_sum(rbegin(ra), rend(ra), rbegin(ra));
transform(begin(rp), end(rp), begin(ra), begin(rp), plus<>{});
return make_pair(h, rp);
}()};
const auto w{[&M]{
vector<unsigned long> w(M);
for(auto&& i : w)cin >> i;
sort(begin(w), end(w));
return w;
}()};
auto it{begin(h)};
unsigned long ans{numeric_limits<unsigned long>::max()};
for(const auto& i : w){
while(it != end(h) && i > *it)++it;
const unsigned long idx{(it - begin(h)) / 2UL};
ans = min(dif[idx] + max(h[2 * idx], i) - min(h[2 * idx], i), ans);
}
cout << ans << endl;
return 0;
}
| #pragma region Region_1
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VP = vector<P>;
#define MOD 1000000007
const int INF = 1e9 + 10;
template <class T, class C>
void chmax(T& a, C b) {
if (a <= b) a = b;
}
template <class T, class C>
void chmin(T& a, C b) {
if (a >= b) a = b;
}
template <class T>
T sum(const vector<T>& v) {
T res = 0;
for (size_t i = 0; i < v.size(); ++i) res += v[i];
return res;
}
/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T>& vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
}
template <class T>
void print(vector<vector<T>>& df) {
for (auto& vec : df) {
print(vec);
}
}
#pragma endregion Region_1
/////////////////////////////////////////////////////////
int main() {
//入力の高速化用のコード
ios::sync_with_stdio(false);
cin.tie(nullptr);
std::cout << std::setprecision(15);
//////////////////////////////////////////
int n, m;
cin >> n >> m;
VL H(n);
VL W(m);
rep(i, n) cin >> H[i];
rep(i, m) cin >> W[i];
sort(rng(H));
VL sum1(n + 1, 0);
for (int i = 0; i < n - 1; i += 2) {
sum1[i / 2 + 1] = sum1[i / 2] + H[i + 1] - H[i];
}
VL sum2(n + 1, 0);
for (int i = n - 2; i > 0; i -= 2) {
sum2[i / 2] = sum2[i / 2 + 1] + H[i + 1] - H[i];
}
ll ans = 2 * INF;
rep(i, m) {
ll w = W[i];
ll idx = [&] {
int l = -1, r = n;
while (l + 1 < r) {
int m = (l + r) / 2;
if (H[m] < w) {
l = m;
} else {
r = m;
}
}
return r;
}();
// print("t1:",w, idx);
// print("t2:",sum1[idx / 2], sum2[idx / 2]);
if (idx % 2 == 0) { // 奇数番目に挿入
chmin(ans, sum1[idx / 2] + sum2[idx / 2] + abs(H[idx] - w));
} else { // 偶数番目に挿入
chmin(ans, sum1[idx / 2] + sum2[idx / 2] + abs(H[idx - 1] - w));
}
}
print(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int t[200005];
long long a[200005];
long long sum[200005];
pair<long long, long long> tree[1000000];
void update(int n, int s, int e, int i, int t, long long v){
if(s == e){
if(t == 2) tree[n].first = v;
else tree[n].second = v;
}
else{
int mid = (s+e)/2;
if(i <= mid) update(n+n, s, mid, i, t, v);
else update(n+n+1, mid+1, e, i, t, v);
tree[n].first = max(tree[n+n].first, tree[n+n+1].first);
tree[n].second = min(tree[n+n].second, tree[n+n+1].second);
}
}
pair<long long, long long> query(int n, int s, int e, int l, int r){
if(s > r || l > e) return make_pair(-1e18, 1e18);
if(l <= s && e <= r) return tree[n];
int mid = (s+e)/2;
pair<long long, long long> x = query(n+n, s, mid, l, r);
pair<long long, long long> y = query(n+n+1, mid+1, e, l, r);
return make_pair(max(x.first, y.first), min(x.second, y.second));
}
int n, q;
int find_max(long long sum, int i){
int lo = i+1, hi = n, ans = -1;
while(lo <= hi){
int mid = (lo+hi)/2;
if(query(1, 1, n, i+1, mid).first >= sum) ans = mid, hi = mid-1;
else lo = mid+1;
}
return ans;
}
int find_min(long long sum, int i){
int lo = i+1, hi = n, ans = -1;
while(lo <= hi){
int mid = (lo+hi)/2;
if(query(1, 1, n, i+1, mid).second <= sum) ans = mid, hi = mid-1;
else lo = mid+1;
}
return ans;
}
long long tmp[200005];
int main(){
scanf("%d", &n);
for(int i=1;i<=4*n;i++) tree[i] = make_pair(-1e18, 1e18);
for(int i=1;i<=n;i++){
scanf("%lld%d", &a[i], &t[i]);
if(t[i] == 1) sum[i] = sum[i-1] + a[i];
else sum[i] = sum[i-1];
a[i] -= sum[i];
}
for(int i=n;i>=1;i--){
if(t[i] == 2 || t[i] == 3){
int mx = find_max(a[i], i);
int mn = find_min(a[i], i);
if(mx == -1 && mn == -1){
tmp[i] = a[i] + sum[i] + (sum[n] - sum[i]);
}
else if(mx != -1 && mn == -1){
tmp[i] = tmp[mx];
}
else if(mn != -1 && mx == -1){
tmp[i] = tmp[mn];
}
else{
tmp[i] = tmp[min(mn, mx)];
}
if(t[i] == 2){
update(1, 1, n, i, 2, a[i]);
}
else{
update(1, 1, n, i, 3, a[i]);
}
}
}
scanf("%d", &q);
for(int i=1;i<=q;i++){
int x;
scanf("%d", &x);
int mx = find_max(x, 0);
int mn = find_min(x, 0);
long long ans = 0;
if(mx == -1 && mn == -1){
ans = x + sum[n];
}
else if(mx != -1 && mn == -1){
ans = tmp[mx];
}
else if(mn != -1 && mx == -1){
ans = tmp[mn];
}
else{
ans = tmp[min(mn, mx)];
}
printf("%lld\n", ans);
}
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cerr << #x << " = " << (x) << endl
#define show2(x,y) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second)
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) (find(all(v),x)!=v.end())
#define incls(s,c) (s.find(c)!=string::npos)
#define lb(a,x) distance((a).begin(),lower_bound(all(a),(x)))
#define ub(a,x) distance((a).begin(),upper_bound(all(a),(x)))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(all(x)),x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
//const int mod = 1000000007;
const double EPS = 1e-9;
//const long double EPS = 1e-14;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
inline int in() { int x; cin >> x; return x;}
inline ll lin() { ll x; cin >> x; return x;}
inline char chin() { char x; cin >> x; return x;}
inline string stin() { string x; cin >> x; return x;}
inline double din() { double x; cin >> x; return x;}
//template<class T = int> inline T in() { T x; cin >> x; return (x);}
template<typename T>inline ll suma(const vector<T>& a) { ll res(0); for (auto&& x : a) res += x; return res;}
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; }
char itoa(int n) { return n + '0';}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
int d[20][20];
int dp[1<<17][17];
int main () {
int n;
cin >> n;
vi x(n), y(n), z(n);
rep(i,n) {
cin >> x[i] >> y[i] >> z[i];
}
rep(i,n)rep(j,n) {
if (i == j) continue;
d[i][j] = INF;
}
rep(s,n)rep(t,n) {
// s -> t
int a = x[s];
int b = y[s];
int c = z[s];
int p = x[t];
int q = y[t];
int r = z[t];
int cost = abs(p-a) + abs(q-b) + max(0,r-c);
d[s][t] = cost;
}
rep(i,1<<n)rep(j,n) dp[i][j] = INF;
dp[(1<<n)-1][0] = 0;
for (int S = (1<<n)-2; S >= 0; S--) {
rep(v,n)rep(u,n) {
if (!(S>>u&1)) {
dp[S][v] = min(dp[S][v], dp[S|1<<u][u] + d[v][u]);
}
}
}
cout << dp[0][0] << endl;
return 0;
} |
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <utility>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 998244353
#define mod1 1610612741
#define mul 31
#define PI 3.14159265358979323846264
//#include bits/stdc++.h
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
typedef long long int lli;
typedef long double ld;
typedef pair<lli,lli> ii;
priority_queue <lli, vector<lli>, greater<lli> > ti;
//priority_queue<pair<lli,pair<lli,lli>>>e;
vector <lli> p[300005],q[300005],f(5000005,0),d(5000005);
//vector<set<lli>>s(200005);
//set<pair<lli,lli>>s;
//lli dp[1000005][3][3];
//lli b[1000005],l[1000005];
//vector<vector<lli>> d(300005,vector<lli>(18,0));
//vector<pair<lli,ii>>p[300005];
map<pair<lli,lli>,lli>mp;
//vector<pair<pair<lli, lli>,lli> > st;
//queue<lli> qy;
lli gcd(lli a, lli b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
lli res = 1;
while (b > 0) {
if (b & 1)
res = (res * a)%mod;
a = (a * a)%mod;
b >>= 1;
}
return res%mod;
}
void fact(lli i)
{
f[0]=1;
for(lli k=1;k<=i;k++)
{
(f[k]=f[k-1]*k)%=mod;
}
}
lli isprime(lli n)
{
if(n==1)
return 0;
for(lli i=2;i<=sqrt(n);i++)
if(n%i==0)
return 0;
return 1;
}
lli find(lli x)
{
if(f[x]==x)
return x;
else
return f[x]=find(f[x]);
}
bool cmp(lli x,lli y)
{
return x<y;
}
void check()
{
cout<<"HI"<<endl;
}
lli comb(lli i,lli j)
{
if(j>i)return 0;
lli k=f[i];
lli g=(f[j]*(f[i-j]))%mod;
lli h=bpow(g,mod-2);
return (k*h)%mod;
}
/*void sieve()
{
for(lli i=2;i<=sqrt(10000000);i++)
{
if(b[i]==0)
{
k.pb(i);
for(lli j=2;i*j<=sqrt(10000000);j++)
{
b[i*j]=1;
}
}
}
}*/
void dfs(lli i,lli pr)
{
for(auto u:p[i])
{
if(f[u]==0)
{
f[u]=f[i]+1;
dfs(u,i);
}
}
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lli n;cin>>n;
lli x;
for(lli i=1;i<=n;i++)
{
cin>>x;
if(i!=x)
{
p[i].pb(x);
p[x].pb(i);
}
}
lli c=0;
for(lli i=1;i<=n;i++)
{
if(f[i]==0)
{
f[i]=1;
dfs(i,0);
c++;
}
//cout<<i<<" "<<c<<endl;
}
lli j=bpow(2,c);
j=(j-1+mod)%mod;
cout<<j<<endl;
}
| #include <iostream>
#include <vector>
#include <set>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int long long
using namespace std;
long long pow_mod(long long x, long long y, long long MOD) {
long long ret = 1;
while (y) {
if (y & 1)
ret = ret * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ret;
}
struct UnionFind {
vector<int> par, siz;
// initialize
UnionFind(int n): par(n, -1), siz(n, 1) {}
int root(int x){
if (par[x] == -1) return x;
else return par[x] = root(par[x]);
}
int issame(int x, int y){
return root(x)==root(y);
}
bool unite(int x, int y){
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x){
return siz[root(x)];
}
};
signed main() {
long long MOD = 998244353;
int N;
cin >> N;
int f[N];
set<int> result_set;
int result;
UnionFind uf(N+1);
rep(i, N){
cin >> f[i];
uf.unite(i+1, f[i]);
}
rep(i, N){
result_set.insert(uf.root(i+1));
}
result = pow_mod(2, result_set.size(), MOD);
cout << result - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define f first
#define s second
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fbo find_by_order
#define ook order_of_key
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << "[" << name << " : " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr << "[";
cerr.write(names, comma - names) << " : " << arg1<<"] | ";__f(comma+1, args...);
}
using ld = long double;
using ll = long long;
using pii = pair <int,int>;
using pll = pair <ll,ll>;
using vi = vector <int>;
using vll = vector <ll>;
using vpii = vector <pii>;
using vpll = vector<pll>;
using vs = vector <string>;
struct UnionFind{
vi parent;
vi set_size;
int n;
UnionFind(int n):n(n){
parent.resize(n+1);
iota(all(parent),0);
set_size.assign(n+1,1);
}
int find(int x){
if(x == parent[x])
return x;
return parent[x] = find(parent[x]);
}
void join(int x,int y){
x = find(x);
y = find(y);
if(x == y)
return;
if(set_size[x] > set_size[y])
swap(x,y);
parent[x] = y;
set_size[y] += set_size[x];
}
};
struct Point{ //Represents a 2D Point
ld x,y;
Point(ld x=0,ld y=0):x(x),y(y){}
};
const ld EPS = 1e-9;
int n;
vector <Point> points;
bool lte(ld x,ld y){ //x <= y
return x < y + EPS;
}
bool lt(ld x,ld y){ // x < y
return x + EPS < y;
}
ld squared(ld x){
return x*x;
}
ld euc_dist(Point p1,Point p2){
return sqrt(squared(p1.x - p2.x) + squared(p1.y - p2.y));
}
bool can(ld m){
UnionFind dsu(n+2);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(lt(euc_dist(points[i],points[j]), 2*m)) dsu.join(i,j);
for(int i=0;i<n;i++){
if(lt(points[i].y + 100, 2*m)) dsu.join(i, n);
if(lt(100 - points[i].y, 2*m)) dsu.join(i, n+1);
}
return dsu.find(n) != dsu.find(n+1);
}
int main(){
fastio;
cin >> n;
points.resize(n);
for(int i=0;i<n;i++)
cin >> points[i].x >> points[i].y;
ld l = 0;
ld r = 100;
ld ans = 0;
for(int i=0;i<100;i++){
ld m = (l + r)/2;
if(can(m)) ans = m, l = m;
else r = m;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int>A(2*N);
vector<pair<int,int>>B(2*N);
vector<int>C(2*N);
for(int i = 0; i < 2*N; i++) {
cin >> A[i];
B[i] = {A[i],i};
}
sort(B.rbegin(),B.rend());
for(int i = 0; i < N; i++) {
C[B[i].second]++;
}
string ans = "";
int sum1 = 0,sum2 = 0;
for(int i = 0; i < 2*N; i++) {
if(C[i] == 0) {
if(sum2) {
sum2--;
ans += ')';
}
else {
sum1++;
ans += '(';
}
}
else {
if(sum1) {
sum1--;
ans += ')';
}
else {
sum2++;
ans += '(';
}
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
# define Z 1000000007
# define el "\n"
# define vt vector
# define pb push_back
# define all(v) (v).begin(),(v).end()
# define ll long long int
int main()
{
ll l;
cin>>l;
vt<ll>v(1<<l);
for(ll i=0;i<(1<<l);i++){
cin>>v[i];
}
auto k1 = max_element(v.begin(),v.begin()+(1<<(l-1)));
auto k2 = max_element(v.begin()+(1<<(l-1)),v.end());
if(*k1>*k2){cout<<k2-v.begin()+1<<el;}else{cout<<k1-v.begin()+1<<el;}
return 0;
}
| #include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
int n;
cin >> n;
vector <long long> a(1 << n);
for (auto &x : a) {
cin >> x;
}
vector <int> id(1 << n);
iota(id.begin(), id.end(), 0);
for (int i = 0; i < n - 1; ++i) {
int f = 1 << (i + 1);
for (int r = f; r <= (1 << n); r += f) {
int l = r - f;
int x = (l + r) / 2 - 1;
int winner = a[id[x]] > a[id[x + 1]] ? id[x] : id[x + 1];
for (int k = l; k < r; ++k) {
id[k] = winner;
}
}
}
int x = (1 << (n - 1)) - 1;
if (a[id[x]] > a[id[x + 1]]) {
cout << id[x + 1] + 1 << endl;
} else {
cout << id[x] + 1 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(ll i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
int main(){
ll R, X, Y; cin >> R >> X >> Y;
ll D = X*X+Y*Y;
if(D < R*R) {cout << "2\n"; return 0;}
ll A = (D+R*R-1)/(R*R);
int ans = 0;
rep2(i, 0, 1000000){
if(A > i*i) ans++;
else break;
}
cout << ans << '\n';
} | //雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define up upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int MOD=998244353;
int n;
int arr[200005];
bool vis[200005];
bool onstack[200005];
bool dfs(int i){
if (vis[i]){
return onstack[i];
}
vis[i]=true;
onstack[i]=true;
bool res=dfs(arr[i]);
onstack[i]=false;
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n;
rep(x,1,n+1) cin>>arr[x];
int cnt=0;
rep(x,1,n+1) if (!vis[x]){
cnt+=dfs(x);
}
ll ans=1;
rep(x,0,cnt){
ans=(ans*2)%MOD;
}
cout<<ans-1<<endl;
}
|
// D - Base n
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
string X;
ll M;
string f(ll n){
ll m = M;
string res = "";
while(m){
ll r = m % n; m = m / n;
res += r <= 9? to_string(r): "a";
}
reverse(res.begin(), res.end());
return res;
}
bool check(ll n){
string m = f(n);
if(X.size() > m.size()) return false;
else if(X.size() < m.size()) return true;
for(int i=0; i<(int)X.size(); ++i)
if(X[i] < m[i]) return true;
else if(X[i] > m[i]) return false;
return true;
}
int main(){
cin>>X>>M;
if(X.size() == 1){ cout<< (stoi(X) <= M) <<endl; return 0; }
int d = 0;
for(auto c:X) d = max(d, c-'0');
ll ok = d, ng = M + 1;
while(ng - ok > 1){
ll n = (ng + ok) / 2;
(check(n)? ok:ng) = n;
}
cout<< ok - d <<endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl "\n"
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void solve(){
int a,b;
cin>>a>>b;
int sum1 =0;
int sum2 =0;
while(a){
int r1 = a%10;
sum1+=r1;
a/=10;
}
while(b){
int r2 = b%10;
sum2 +=r2;
b/=10;}
if (sum1>sum2)
cout<<sum1<<endl;
else cout<<sum2<<endl;
}
signed main(){
IOS
solve();
return 0;
} |
#include<bits/stdc++.h>
#define N 505
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
bool mmp1;
int n,m,K;
namespace P100 {
int C[N][N];
int A[N],B[N];
int D[N][N];
void solve() {
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&C[i][j]);
for(int i=1;i<=n;i++)
B[i]=C[1][i];
for(int i=1;i<=n;i++)
A[i]=C[i][1]-B[1];
int ma=A[1],mb=B[1];
for(int i=2;i<=n;i++)ma=min(ma,A[i]);
for(int i=2;i<=n;i++)mb=min(mb,B[i]);
if(ma+mb<0){
puts("No");
return;
}else if(ma<0)for(int i=1;i<=n;i++)A[i]-=ma,B[i]+=ma;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
// printf("D[%d][%d]=%d\n",i,j,A[i]+B[j]);
if(C[i][j]!=A[i]+B[j]){
puts("No");
return;
}
}
puts("Yes");
for(int i=1;i<=n;i++)
printf("%d ",A[i]);
puts("");
for(int i=1;i<=n;i++)
printf("%d ",B[i]);
}
}
bool mmp2;
int main() {
// srand(time(0));
// printf("%.6fMB\n",(&mmp2-&mmp1)/1024.0/1024);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
P100::solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define length size()
#define int long long
#define ll long long
#include <cstdint>
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
if(n==0) return 0;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
const int MOD = 1000000007;
const int mod = 998244353;
const int MAX = 510000;
const int inf = 400000000000000;
template<typename T> string join(vector<T> &vec ,const string &sp){
int si = vec.length;
if(si==0){
return "";
}else{
stringstream ss;
rep(i,si-1){
ss << vec[i] << sp;
}
ss << vec[si - 1];
return ss.str();
}
}
vector<vector<int>> ruiseki2d(vector<vector<int>> vec){
rep(i,vec.size()-1){
rep(j,vec[0].size()-1){
vec[i+1][j+1] = vec[i][j+1]+vec[i+1][j]-vec[i][j]+vec[i+1][j+1];
}
}
return vec;
}
class CompareDist
{
public:
bool operator()(pair<int,int> p,pair<int,int>q){
return p.second > q.second;
}
};
int ctoi(const char c){
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default : return -1;
}
}
vector<int> dijkstra(vector<vector<pair<int,int>>> vec,int s){
priority_queue<pair<int,int>,vector<pair<int,int>>,CompareDist> que;
pair<int,int> f = make_pair(s,0);//point,cost;
que.push(f);
bool y = false;
vector<int> cost(vec.size(),inf);
while(!que.empty()){
pair<int,int> tmp = que.top();
que.pop();
int p = tmp.first;
int c = tmp.second;
if(cost[p]>c){
cost[p] = c;
rep(i,vec[p].size()){
if(cost[vec[p][i].first]<=inf){
tmp = make_pair(vec[p][i].first,vec[p][i].second+c);
que.push(tmp);
}
}
}
if(!y) cost[p] = inf;
y = true;
}
return cost;
}
signed main(void){
int n;
cin >> n;
vector<vector<int>> vec(n,vector<int>(n));
rep(i,n){
rep(j,n){
cin >> vec[i][j];
}
}
vector<int> vx(n,0);
vector<int> vy(n,0);
bool flag = false;
rep(i,n-1){
int t = vec[0][i+1]-vec[0][i];
rep(j,n-1){
if(vec[j+1][i+1]-vec[j+1][i]!=t) flag = true;
}
vx[i+1] = t;
}
rep(i,n-1){
int t = vec[i+1][0]-vec[i][0];
// cout << t << " ";
rep(j,n-1){
if(vec[i+1][j+1]-vec[i][j+1]!=t) flag = true;
}
vy[i+1] = t;
}
rep(i,n-1){
vx[i+1] += vx[i];
vy[i+1] += vy[i];
}
// cout << join(vx," ") << endl;
// cout << join(vy," ") << endl;
if(flag) cout << "No" << endl;
else{
//cout << "Yes" << endl;
int xm = *min_element(vx.begin(),vx.end());
int ym = *min_element(vy.begin(),vy.end());
xm *= -1;
ym *= -1;
// cout << xm << " " << ym << endl;
rep(i,n){
vx[i] += xm;
vy[i] += ym;
}
// cout << join(vx," ") << endl;
// cout << join(vy," ") << endl;
if(vx[0]+vy[0]>vec[0][0]){cout << "No" << endl;}
else{
int k = (vec[0][0]-(vx[0]+vy[0]));
rep(i,n){
vx[i] += k;
}
cout << "Yes" << endl;
cout << join(vy," ") << endl;
cout << join(vx," ") << endl;
}
}
}
|
#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define DISPLAY(V) for(auto it : V) { cout<<it<<" " ;} cout<<endl;
#define DISPLAY2(V) for(auto it : V) { for (auto val : it) { cout<<val<<" ";} cout<<endl;}
#define MOD 1000000007
#define see(x) cout<<#x<< " = "<<x<<endl;
#define seep(x) cout<<#x<<" = "<<"["<<x.first<<","<<x.second<<"]"<<endl;
inline int add(int a,int b){a+=b;if(a>=MOD)a-=MOD;return a;}
inline int mul(int a,int b){return (a*1ll*b)%MOD;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
void solve()
{
ll n ;
cin>>n ;
if(n%4==2)
{
cout<<"Same\n" ;
}
else if(n%2==0) cout<<"Even\n";
else cout<<"Odd\n" ;
}
int main()
{
fast ;
ll t = 1;
cin>>t;
for(ll i = 1 ;i<=t; ++i)
{
solve();
}
return 0 ;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
if(n%2!=0)
cout<<"Odd"<<"\n";
else if(n%4==0)
{
cout<<"Even"<<"\n";
}
else
cout<<"Same"<<"\n";
}
return 0;
} |
//a.9
#include<bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end()
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define endll endl
#define endl '\n'
#define mod 1000000007
#define INF 1e18
#define maxN 500005
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
ll ceill(ll a,ll b){return a/b+bool(a%b);}
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);}
ll power(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}
bool isPrime(ll n)
{
if(n<=1)return false;if(n<=3)return true;if(n%2==0 || n%3==0)return false;
for(ll i=5;i*i<=n;i=i+6)if(n%i==0 || n%(i+2)==0) return false; return true;
}
void primeFactors(ll n, vector<ll> &vec)
{
while(n%2==0)vec.push_back(2),n/=2;
for(ll i=3;i<=sqrt(n);i+=2)while(n%i==0)vec.push_back(i),n/=i;
if(n>2)vec.push_back(n);
}
//vector<ll>fact(maxN);
//void factpre(){fact[0]=1;for(ll i=1;i<maxN;i++)fact[i]=(fact[i-1]*1LL*i);}
//ll mul(ll a,ll b){return (a*1LL*b)%mod;}
//ll nCr(ll n,ll k){return mul(fact[n],power(mul(fact[k],fact[n-k]),mod-2));}
void solve()
{
char a,b,c;cin>>a>>b>>c;
cout<<b<<c<<a<<endl;
}
signed main()
{
ios;
ll tc=1; //cin>>tc;
for(ll TTT=1;TTT<=tc;TTT++)
{
//CASE(TTT);
solve();
}
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++i)
template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;}
int main(){
string s;
cin >>s;
cout << s[1] << s[2] << s[0] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define vll vector<long long>
#define mll map<long long,long long>
#define pll pair<long long,long long>
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 998244353
typedef long long ll;
typedef long double ld;
long double pi=3.14159265358979323846;
vll al[300005];
ll I[101][101];
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
bool isPrime(int n)
{
if (n<=1) return false;
if (n<=3) return true;
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 bpow(ll n, ll po)
{
ll res=1;
while(po>0)
{
if(po%2)
{
res=(res*n)%mod;
po--;
}
else
{
n=(n*n)%mod;
po/=2;
}
}
return res;
}
void swap(ll *a, ll *b)
{
ll temp=*a;
*a=*b;
*b=temp;
}
ll fact(ll n) {
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
}
ll modInverse(ll n,ll p)
{
return bpow(n,p-2);
}
ll fac[300001];
ll nCr(ll n,ll r)
{
if (r == 0) return 1;
return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod) % mod) % mod;
}
//bool bipartite_dfs(ll node, ll flag)
//{
// vis[node]=1;
// ll cflag=flag;
// color[node]=cflag;
// for(ll child : al[node])
// {
// if(!vis[child])
// {
// bool n=bipartite_dfs(child,1-cflag);
// if(n==false) return false;
// }
// else if(color[child]==color[node]) return false;
// }
// return true;
//}
void mmul(ll A[][101], ll B[][101], ll dim)
{
ll res[dim][dim];
for(ll i=0;i<dim;i++) for(ll j=0;j<dim;j++)
{
res[i][j]=0;
for(ll k=0;k<dim;k++) res[i][j]=(res[i][j]+A[i][k]*B[k][j])%1000000007;
}
for(ll i=0;i<dim;i++) for(ll j=0;j<dim;j++) A[i][j]=res[i][j];
}
void mpow(ll A[][101],ll dim, ll po)
{
for(ll i=0;i<dim;i++) for(ll j=0;j<dim;j++)
{
if(i==j) I[i][j]=1;
else I[i][j]=0;
}
while(po>0)
{
if(po%2==1)
{
mmul(I,A,dim);
po--;
}
else
{
mmul(A,A,dim);
po/=2;
}
}
for(ll i=0;i<dim;i++) for(ll j=0;j<dim;j++) A[i][j]=I[i][j];
}
ll b[200001],vis[200001];
ll dfs(ll node){
vis[node]=1;
ll ret=1;
for(ll i=0;i<al[node].size();i++){
ll child=al[node][i];
if(!vis[child]){
ret+=dfs(child);
}
}
return ret;
}
int main()
{
FAST
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll testcases=1;
// cin>>testcases;
// fac[0]=1;
// for(ll i=1;i<300001;i++) fac[i]=(fac[i-1]*i)%mod;
while(testcases--)
{
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++) cin>>a[i];
for(ll i=0;i<n;i++){
ll x=a[i],y=a[n-1-i];
if(x!=y) al[x].pb(y);
}
for(ll i=0;i<n;i++) b[a[i]]++;
ll ans=0;
for(ll i=1;i<=200000;i++) if(!vis[i]) ans+=dfs(i)-1;
cout<<ans;
}
}
|
// Problem : D - KAIBUNsyo
// Contest : AtCoder - AtCoder Beginner Contest 206(Sponsored by Panasonic)
// URL : https://atcoder.jp/contests/abc206/tasks/abc206_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
/* DEBUGGING */
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
/* MACROS */
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef map<int,int> mii;
typedef tuple<int,int,int> tup;
#define ff first
#define ss second
#define pb push_back
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define ps(x,y) fixed<<setprecision(y)<<x
#define setbit(x) __builtin_popcountll(x)
#define rep(i,a,b) for(int i=a ; i<b ; ++i)
#define repr(i,a,b) for(int i=a ; i>=b ; --i)
/* CONSTANTS */
#define PI 3.141592653589793
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 2e5 + 2;
vi adj[MAX_N],vis(MAX_N);
int sz;
void dfs(int node){
vis[node] = 1;
sz++;
for(auto x:adj[node]){
if(!vis[x]){
dfs(x);
}
}
}
void solve(){
int n; cin >> n;
set<int>s;
fill(all(vis) , 0);
vi v(n);
rep(i,0,n) cin >> v[i], s.insert(v[i]);
int ans = 0;
rep(i,0,n){
if(v[i]!=v[n-i-1]){
adj[v[i]].pb(v[n-i-1]);
adj[v[n-i-1]].pb(v[i]);
}
}
rep(i,1,MAX_N){
if(!vis[i]){
sz = 0;
dfs(i);
ans+=(sz-1);
}
}
cout << min(ans , (int)s.size());
}
int main(){
IOS;
solve();
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll> > vii;
typedef pair<ll,ll> pii;
typedef multiset<pair<ll, ll> > msii;
typedef multiset<ll> msi;
typedef set<pair<ll, ll> > sii;
typedef set<ll> si;
typedef map<ll, ll> mii;
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define for1(x, a, b) for(x=a; x<b; x++)
#define for2(x, a, b) for(x=a, x>=b; x--)
//#define endl "\n"
#define all(x) x.begin(), x.end()
#define p pair
#define mp make_pair
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define pb push_back
#define ff first
#define ss second
#define bb begin
#define arr_ub(arr, n, x) upper_bound(arr, arr+n, x)-arr
#define arr_lb(arr, n, x) lower_bound(arr, arr+n, x)-arr
#define u_p(v, x) upper_bound(v.begin(), v.end(), x)-v.begin()
#define l_b(v, x) lower_bound(v.begin(), v.end(), x)-v.begin()
const ll sz=1e6+123;
#define INF 1000000000000000007
#define MOD 1000000007
#define stringLen 18446744073709551620
#define pi 3.1415926536
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
ll n;
int main(){
cin >> n;
vector<vector<ll>> T(n, vector<ll>(3)); //l, r, t
for(int i=0; i<n; ++i){
vector<ll> tmp(3);
cin >> tmp[2] >> tmp[0] >> tmp[1];
T[i] = tmp;
}
sort(all(T), [](const vector<ll>& a, const vector<ll>& b){
return a[0] < b[0];
});
ll ans = 0;
for(int i=0; i<n-1; ++i){
int j= i+1;
while(( T[i][1] >= T[j][0] || T[i][0] == T[j][0] )&& j<n){
// if (T[i][0] == T[j][0]){
// if(T[i][2]==3 || T[i][2]==4 || T[j][2] == 3 || T[j][2] == 4) {
// j++;
// continue;
// }
// }
if (T[i][1] == T[j][0]){
if(T[i][2]==2 || T[i][2]==4 || T[j][2] == 3 || T[j][2] == 4) {
j++;
if(j >= n){
break;
}
continue;
}
}
ans++;
j++;
if(j>=n){
break;
}
}
}
cout << ans;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
int main()
{
int N; cin >> N;
double l[N], r[N];
for (int i = 0; i < N; i++)
{
int t; cin >> t >> l[i] >> r[i];
t--;
if (t & 1) r[i] -= 0.5;
if (t & 2) l[i] += 0.5;
}
int ans = 0;
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
ans += (max(l[i], l[j]) <= min(r[i], r[j]));
}
}
cout << ans << '\n';
return 0;
} |
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n; cin >> n;
vector<ll> r, g, b;
for (int i = 0; i < 2 * n; i++) {
ll a; char c; cin >> a >> c;
if (c == 'R') { r.push_back(a); }
else if (c == 'G') { g.push_back(a); }
else { b.push_back(a); }
}
vector<ll> v1, v2, v3;
if (r.size() % 2 && g.size() % 2) { v1 = r; v2 = g; v3 = b; }
else if (r.size() % 2 && b.size() % 2) { v1 = r; v2 = b; v3 = g; }
else if (g.size() % 2 && b.size() % 2) { v1 = g; v2 = b; v3 = r; }
else { cout << 0 << '\n'; return 0; }
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
sort(v3.begin(), v3.end());
ll ans = 1e16;
ll min12 = 1e16, min13 = 1e16, min23 = 1e16;
for (int i = 0; i < v1.size(); i++) {
int cur = lower_bound(v2.begin(), v2.end(), v1[i]) - v2.begin();
if (cur < v2.size()) { min12 = min(min12, v2[cur] - v1[i]); }
if (cur > 0) { min12 = min(min12, v1[i] - v2[cur - 1]); }
}
for (int i = 0; i < v1.size(); i++) {
int cur = lower_bound(v3.begin(), v3.end(), v1[i]) - v3.begin();
if (cur < v3.size()) { min13 = min(min13, v3[cur] - v1[i]); }
if (cur > 0) { min13 = min(min13, v1[i] - v3[cur - 1]); }
}
for (int i = 0; i < v2.size(); i++) {
int cur = lower_bound(v3.begin(), v3.end(), v2[i]) - v3.begin();
if (cur < v3.size()) { min23 = min(min23, v3[cur] - v2[i]); }
if (cur > 0) { min23 = min(min23, v2[i] - v3[cur - 1]); }
}
ans = min(min12, min13 + min23);
cout << ans << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using LD = long double;
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
template <typename T> void DEBUGall(T v) { if(v.empty()){cerr << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cerr << *i << " ";} cerr << *--v.end() << endl; }
inline void IN(void){ return; }
template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; }
inline void OUT(void){ cout << "\n"; return; }
template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " "; OUT(rest...); return; }
inline void DEBUG(void){ cerr << "\n"; return; }
template <typename First, typename... Rest> void DEBUG(First first, Rest... rest){ cerr << first << " "; DEBUG(rest...); return; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); }
ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); }
const ll INF = 1002003004005006001;
void Main(){
int n; IN(n);
vector<vll> v(3);
rep(i, n*2) {
ll a; char c;
IN(a, c);
if(c == 'R') v[0].pb(a);
if(c == 'G') v[1].pb(a);
if(c == 'B') v[2].pb(a);
}
if(v[0].size() % 2 == 0 && v[1].size() % 2 == 0 & v[2].size() % 2 == 0){
OUT(0);
return;
}
if(v[1].size() % 2 == 0) swap(v[0], v[1]);
else if(v[2].size() % 2 == 0) swap(v[0], v[2]);
rep(i, 3) sort(all(v[i]));
/*
DEBUGall(v[0]);
DEBUGall(v[1]);
DEBUGall(v[2]);
*/
// v[0]: even, v[1], v[2]: odd
auto f = [&](vll a, vll b){
ll res = INF, ans = INF;
for(auto x : a){
auto it = upper_bound(all(b), x);
if(it != b.end()){
if(chmin(res, abs(x-*it))) ans = x;
}
if(it != b.begin()){
it--;
if(chmin(res, abs(x-*it))) ans = x;
}
}
return Pll{res, ans};
};
auto [r1, a1] = f(v[1], v[2]);
auto [r2, a2] = f(v[0], v[1]);
auto [r3, a3] = f(v[0], v[2]);
/*
DEBUG(r1, a1);
DEBUG(r2, a2);
DEBUG(r3, a3);
*/
if(a2 != a3 || a2 == INF){
OUT(min(r1, r2 + r3));
return;
}
vll w = v[0];
w.erase(find(all(w), a2));
auto [r4, a4] = f(w, v[1]);
auto [r5, a5] = f(w, v[2]);
ll ANS = min(r3 + r4, r2 + r5);
OUT(min(r1, ANS));
return;
}
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> Pair;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
int dp[101][1001001], sum[101][101][10101];
void Main(){
int N,K,M; cin >> N >> K >> M;
VI s(N,0); REP(i,N) s[i] = s[max(0,i-1)] + (i+1)*K;
dp[0][0] = 1; REP(i,N*N) sum[0][0][i+1] = 1;
REP(i,N){
REP(j,s[i]+1){
int mod = j%(i+1), div = j/(i+1);
(dp[i+1][j] += sum[i][mod][div+1] - sum[i][mod][max(0,div-K)]) %= M;
if(dp[i+1][j] < 0) dp[i+1][j] += M;
mod = j%(i+2); div = j/(i+2);
sum[i+1][mod][div+1] += dp[i+1][j];
if(sum[i+1][mod][div+1]>=M) sum[i+1][mod][div+1] -= M;
}
REP(j,i+2)REP(k,N*N){
sum[i+1][j][k+1] += sum[i+1][j][k];
if(sum[i+1][j][k+1]>=M) sum[i+1][j][k+1] -= M;
}
}
FOR(x,1,N+1){
ll ans = 0;
REP(j,N*N*K+1){
(ans += 1LL*dp[N-x][j]*dp[x-1][j]%M*(K+1)) %= M;
}
ans--;
if(ans<0) ans += M;
cout << ans << en;
}
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
while(t--) Main();
return 0;
} | /*
author:ryo3ihara
”継続は力なり、雨だれ石を穿つ”
”slow but steady wins the race”
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
/*
// 多倍長テンプレ
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;
*/
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pii = pair<int, int>;
using pld = pair<ll, ld>;
using ppiii = pair<pii, int>;
using ppiill = pair<pii, ll>;
using ppllll = pair<pll, ll>;
using pplii = pair<pli, int>;
using mii = map<int, int>;
using dll = deque<ll>;
using qll = queue<ll>;
using pqll = priority_queue<ll>;
using pqrll = priority_queue<ll, vector<ll>, greater<ll>>;
using vint = vector<int>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvint = vector<vector<int>>;
using vvpll = vector<vector<pll>>;
//マクロ
//forループ
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
const ll MOD = 1000000007;
const ll INF = 1000000000000; //10^12:∞
const ll MAXR = 100000; //10^5:配列の最大のrange
//最大化問題最小化問題
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;
}
//高速冪乗化 modの時は一部入れ替える
ll power(ll x, ll y) {
if(y == 1) {
return x;
}
ll ans;
if(y % 2 == 1) {
ll r = power(x,(y-1)/2);
//ans = r * r % MOD;
//ans = ans * x % MOD;
ans = x * r * r;
}
else {
ll r = power(x,y/2);
//ans = r * r % MOD;
ans = r * r;
}
return ans;
}
/*
Bint powerb(Bint x, Bint y) {
if(y == 1) {
return x;
}
Bint ans;
if(y % 2 == 1) {
Bint r = powerb(x,(y-1)/2);
//ans = r * r % MOD;
//ans = ans * x % MOD;
ans = x * r * r;
}
else {
Bint r = powerb(x,y/2);
//ans = r * r % MOD;
ans = r * r;
}
return ans;
}
*/
signed main(){
//入力の高速化用のコード
ios::sync_with_stdio(false);
cin.tie(nullptr);
//入力
ll N,M;
cin >> N >> M;
vll H(N),W(M);
REP(i,N) cin >> H[i];
REP(i,M) cin >> W[i];
sort(ALL(H));
sort(ALL(W));
vll left((N+1)/2);
vll right((N+1)/2);
for(int i =0;i+1<N;i+=2){
left[i/2+1] = left[i/2] + H[i+1] - H[i];
}
for(int i =N-2;i>0;i-=2){
right[i/2] = right[i/2+1] + H[i+1]-H[i];
}
vll dist(N);
ll ans = INF;
REP(i,M){
ll x = lower_bound(H.begin(),H.end(),W[i])-H.begin();
if(x & 1) x ^= 1;
chmin(ans,left[x/2] + right[x/2] + abs(H[x] - W[i]));
}
//本文
//出力
cout << ans << endl;
//cout << fixed << setprecision(10) << ans << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>
using namespace std;
#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
//#define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template <typename T>
std::string printVector(const std::vector<T> &data)
{
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
template <typename T>
void print_array(const T &ary, int size){
REP(i,size){
cout << ary[i] << " ";
}
cout << endl;
}
const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
VI C;
VVI g;
VI ans;
void dfs(int v, int p, multiset<int>& se){
if(se.find(C[v]) == se.end()){
ans.eb(v);
}
for(int u : g[v]){
if(u == p) continue;
se.insert(C[v]);
dfs(u,v,se);
se.erase(se.find(C[v]));
}
}
signed main(int argc, char const *argv[])
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(12);
int N; cin >> N;
C.resize(N);
REP(i,N) cin >> C[i];
g.resize(N);
REP(i,N-1){
int a,b; cin >> a >> b;
a--; b--;
g[a].eb(b);
g[b].eb(a);
}
multiset<int> se;
dfs(0,-1, se);
sort(ans.begin(), ans.end());
for(int x : ans){
cout << x + 1 << "\n";
}
} | #include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t){
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}}
#define debugr(l,r,x) {{cerr<<#x<<": "; for(int i=l;i<=r;i++) cerr<<x<<" "; cerr<<endl;}}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debugr(l,r,x)
#define cerr while(0) cerr
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define int long long
typedef __int128 INT;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int a[500005];
bool good[500005];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string ans="";
int n;
cin>>n;
n*=2;
for(int i=0; i<n; i++) ans.push_back('#');
for(int i=0; i<n; i++){
cin>>a[i];
}
vi idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int x, int y){
return a[x] < a[y];
});
for(int i=0; i<n/2; i++){
good[idx[i]]=1;
}
stack<int> st;
for(int i=0; i<n; i++){
if(st.empty()){
ans[i]='(';
st.push(i);
}
else{
int t=st.top();
if(good[t]==good[i]){
st.push(i);
}
else{
st.pop();
ans[t]='(';
ans[i]=')';
}
}
}
cout<<ans<<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 int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define lb lower_bound
#define ub upper_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a,b)))
#define inf (int)(4e18)
#define endl '\n'
#define mp make_pair
#define time(s) (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}
template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}
template<typename T> void Unique(vector<T> &v) {
sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}
void solve() {
int n;
cin >> n;
int x[n], y[n], z[n];
for(int i = 0; i < n; i++) {
cin >> x[i] >> y[i] >> z[i];
}
auto cal = [&](int i, int j) {
return abs(x[i]-x[j]) + abs(y[i]-y[j]) + max(0LL, z[j]-z[i]);
};
int dp[n][(1<<n)];
for(int i = 0; i < n; i++) {
for(int j = 0; j < (1<<n); j++)
dp[i][j] = inf;
}
dp[0][1] = 0;
for(int j = 3; j < (1<<n); j++) {
if(!(j&1)) continue;
for(int i = 0; i < n; i++) {
if((j>>i)&1) {
for(int k = 0; k < n; k++) {
if(i != k)
dp[i][j] = min(dp[i][j], dp[k][j^(1<<i)] + cal(k, i));
}
}
}
}
// cout << dp[0][(1<<n)-1] << endl;
// cout << dp[1][(1<<n)-1] << endl;
int ans = inf;
for(int j = 0; j < n; j++) {
ans = min(ans, dp[j][(1<<n)-1] + cal(j, 0));
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
start = clock();
int test = 1;
// cin >> test;
cout << fixed << setprecision(20);
for(int i = 1; i <= test; ++i){
solve();
}
cerr << time(start);
return 0;
}
| /**
* author: longvu
**/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define FASTIO std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
#define nax (int) (100001)
const int mod = 1e9 + 7;
int32_t main()
{
FASTIO
int n;
cin >> n;
string s1, s2;
cin >> s2;
s1 = s2.substr(0, n);
s2 = s2.substr(n, n);
int q;
cin >> q;
while (q--)
{
int type, a, b;
cin >> type >> a >> b;
if (type == 1)
{
a--; b--;
char c;
int flag = 0;
if (a > s1.size() - 1)
{
flag = 1;
a -= s1.size();
c = s2[a];
}
if (b > s1.size() - 1)
{
b -= s1.size();
if (flag)
{
s2[a] = s2[b];
s2[b] = c;
}
else
{
c = s2[b];
s2[b] = s1[a];
s1[a] = c;
}
}
else
{
c = s1[a];
s1[a] = s1[b];
s1[b] = c;
}
}
else
{
swap(s1, s2);
}
}
cout << s1 + s2;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
string s;cin>>s;
int i;
int an[10];rep(i,10)an[i]=0;
for (auto c: s) {
i = c - '0';
if (an[i]<3) an[i]++;
}
vi bn;
rep(i, 10) {
rep(j, an[i]) bn.push_back(i);
}
int n = bn.size();
if (n == 1) {
if (bn[0] % 8 == 0) {
cout<<"Yes"<<endl;
return 0;
}
} else if (n == 2) {
if ((bn[0]*10 + bn[1]) % 8 == 0 or (bn[1]*10 + bn[0]) % 8 == 0) {
cout<<"Yes"<<endl;
return 0;
}
} else {
rep(i,n) rep(j,n) rep(k,n) {
if (i==j or j==k or k==i) continue;
if ((bn[i]*100 + bn[j]*10 + bn[k]) % 8 == 0) {
cout<<"Yes"<<endl;
return 0;
}
}
}
cout<<"No"<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
string k;
cin >> k;
if(k.length() == 1){
if(k[0] == '8'){
cout << "Yes";
return 0;
}
cout << "No";
return 0;
}
if(k.length() == 2){
if((k[0] * 10 + k[1]) % 8 == 0 || (k[1] * 10 + k[0]) % 8 == 0){
cout << "Yes";
return 0;
}
cout << "No";
return 0;
}
int bucket[10] = {0};
for(int i = 0;i < k.length();i++){
bucket[k[i] - '0']++;
}
for(int i = 112;i <= 1000;i += 8){
if(i % 10 == 0 || i / 10 % 10 == 0){
continue;
}
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
int b1[10] = {0};
b1[ge]++;
b1[shi]++;
b1[bai]++;
bool ok = true;
for(int i = 1;i <= 9;i++){
if(b1[i] > bucket[i]){
ok = false;
break;
}
}
if(ok){
cout << "Yes";
return 0;
}
}
cout << "No";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N;
vector<vector<char>> tree(N, vector<char>(M));
int a, b;
//for (int i = 0; i < M; i++){
int seiki;
if (N % 100 == 0)
seiki = N / 100;
else
seiki = N / 100 + 1;
cout << seiki << endl;
}
| #include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cctype>
#include <limits> // INT_MAX ...
using namespace std;
struct fast_ios {
fast_ios() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << fixed << setprecision(10);
}
} fast_ios_;
int main() {
int y;
cin >> y;
cout << (y + 99) / 100 << '\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> r(n);
r[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 2 * i; j <= n; j += i) {
if (r[i - 1] + 1 > r[j - 1]) r[j - 1] = r[i - 1] + 1;
}
}
for (auto e : r) cout << e << ' ';
cout << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
int n; cin>>n;
cout << 1 << " \n"[n == 1];
int num = 1;
int dex = 2;
int ind = 2;
int i = 0;
while (num < n)
{
if (i == dex)
{
i = 0;
dex *= 2;
ind++;
}
cout << ind << " \n"[num == n - 1];
i++;
num++;
}
cout << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(n) begin(n),end(n)
typedef vector<int> vi;
typedef vector<string> vs;
const int mod = 1000000007;
const int inf = 1061109567;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
signed main()
#define N_MAX 200200
{
int n;
cin >> n;
vi p(n);
vector<bool> check(N_MAX, false);
set <int> s;
int x;
int ans = 0;
rep(i,n){
cin >> x;
if(check[x]) { cout << ans << endl; continue;}
check[x] = true;
while(1){
if(!check[ans]) { cout << ans << endl; break;}
ans++;
}
}
return 0;
} | #define _GLIBCXX_DEBUG//これが最後の手段だ!
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<numeric>
#include<climits>
#include<limits>//
#include <stdio.h>//
#include<cmath>
#include<iomanip>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
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); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n,m = 0;
cin >> n;
vector<int> p(n),a(200002),b(200002);
for (int i = 1; i <= 200001; i++){
a[i] = i;
}
for (int i = 0; i < n; i++){
cin >> p[i];
b[p[i]] = 1;
while(b[m] == 1) ++m;
cout << m << '\n';
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define pqueue priority_queue
#define umap unordered_map
#define uset unordered_set
#define bit(s, i) ((1 << i) & s ? 1 : 0)
#define bits(x) __builtin_popcount((x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x) / gcd(x, y) * (y))
#define inf 4557430888798830399LL
#define mst(dp, val) memset(dp, val, sizeof(dp))
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repd(i, a, b, d) for (int i = a; i <= b; i += d)
#define rep_(i, a, b) for (int i = a; i >= b; --i)
#define repd_(i, a, b) for (int i = a; i >= b; i -= d)
#define mxm(a, b) a = max(a, b)
#define mnm(a, b) a = min(a, b)
#define Max *max_element
#define Min *min_element
#define pb push_back
#define X first
#define Y second
#define L size()
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << "=" << x << endl
#define readArr(a, n) for (int i = 1; i <= n; i++) cin >> a[i];
#define printArr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl;
#define print(e) cout << (e) << endl
const int mod = 1e9 + 7;
#define Add(x, val) x = (x + (val)) % mod
// #define MULTI_CASES
// #define PRE
const int N = 2e5 + 5;
int a[N];
void solve() {
int n; cin >> n;
readArr(a, n);
int mx = 0;
int preSum = 0;
int h = 0;
rep(k, 1, n) {
mx = mxm(mx, a[k]);
preSum += a[k];
h += preSum;
print(k*mx + h);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#ifdef PRE
pre();
#endif
#ifdef MULTI_CASES
int t; cin >> t;
while (t--) solve();
#else
solve();
#endif
return 0;
} | #include<iostream>
using namespace std;
long n,a[200020],rui[200020];
main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>a[i],rui[i]=i==0?a[i]:a[i]+rui[i-1];
long ans=0,M=0;
for(int i=0;i<n;i++)M=max(M,a[i]),ans+=rui[i],cout<<(ans+M*(i+1))<<'\n';
} |
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
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;}
template<class T> inline void prn(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << ' ';}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printvm(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i].val() << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvvm(vector<vector<T>>& v) {for (auto u : v) printvm(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
}
void solve() {
int n;
cin >> n;
ll a;
char c;
vl r, g, b;
REP(i, n * 2) {
cin >> a >> c;
if (c == 'R') r.emplace_back(a);
else if (c == 'G') g.emplace_back(a);
else if (c == 'B') b.emplace_back(a);
}
SORT(r);
SORT(g);
SORT(b);
if (sz(r) % 2 == 0 and sz(g) % 2 == 0 and sz(b) % 2 == 0) {
cout << 0 << endl;
return;
}
ll rg = LINF;
for (auto e : g) {
auto it = lower_bound(ALL(r), e);
if (it == r.end()) chmin(rg, abs(r[sz(r) - 1] - e));
else if (it == r.begin()) chmin(rg, abs(r[0] - e));
else {
int idx = distance(r.begin(), it);
chmin(rg, abs(r[idx - 1] - e));
chmin(rg, abs(r[idx] - e));
}
}
ll rb = LINF;
for (auto e : b) {
auto it = lower_bound(ALL(r), e);
if (it == r.end()) chmin(rb, abs(r[sz(r) - 1] - e));
else if (it == r.begin()) chmin(rb, abs(r[0] - e));
else {
int idx = distance(r.begin(), it);
chmin(rb, abs(r[idx - 1] - e));
chmin(rb, abs(r[idx] - e));
}
}
ll gb = LINF;
for (auto e : b) {
auto it = lower_bound(ALL(g), e);
if (it == g.end()) chmin(gb, abs(g[sz(g) - 1] - e));
else if (it == g.begin()) chmin(gb, abs(g[0] - e));
else {
int idx = distance(g.begin(), it);
chmin(gb, abs(g[idx - 1] - e));
chmin(gb, abs(g[idx] - e));
}
}
if (sz(r) % 2 == 0) cout << min(rg + rb, gb) << endl;
else if (sz(g) % 2 == 0) cout << min(rg + gb, rb) << endl;
else if (sz(b) % 2 == 0) cout << min(rb + gb, rg) << endl;
}
| #include<bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define pb push_back
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){a=a<b?b:a;}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){a=a<b?a:b;}
const int N=100005;
typedef long long ll;
std::vector<ll> a[3];
ll last[3];
ll calc(const std::vector<ll> &a,const std::vector<ll> &b){
std::vector<std::pair<ll,int>> v;
for(auto it:a)v.pb({it,0});
for(auto it:b)v.pb({it,1});
std::sort(v.begin(),v.end());
last[0]=last[1]=-1e18;
ll ans=1e18;
for(auto [x,y]:v)chmin(ans,x-last[!y]),last[y]=x;
return ans;
}
int main(){
int n;read(n);
for(int i=1;i<=n+n;++i){
ll x;read(x);
char ch;do{ch=getchar();}while(!isalpha(ch));
a[ch=='R'?0:ch=='G'?1:2].pb(x);
}
if(SZ(a[0])%2==0&&SZ(a[1])%2==0&&SZ(a[2])%2==0)return puts("0"),0;
int p=-1;for(int i=0;i<3;++i)if(SZ(a[i])%2==0)p=i;
std::vector<ll> x,y,z=a[p];
if(p==0)x=a[1],y=a[2];
else if(p==1)x=a[0],y=a[2];
else if(p==2)x=a[0],y=a[1];
printf("%lld\n",std::min(calc(x,y),calc(x,z)+calc(y,z)));
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <math.h>
#include <limits.h>
#include <string>
#include <bitset>
using namespace std;
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define manytests int TT;cin >> TT; while (TT--)
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FORd(i,a,b) for (int i = (a); i >= (b); --i)
int dx[8] = {-1, 1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, 1, -1, -1};
#define ll long long
#define ld long double
#define fi first
#define se second
#define rev reverse
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define vd vector<double>
#define vs vector<string>
#define vld vector<ld>
#define vb vector<bool>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
cin >> a >> b;
int x = 0, y = 0;
while (a) {
x += a%10;
a/=10;
}
while (b) {
y += b%10;
b/=10;
}
cout << max(x, y) << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
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; }
const ll MOD = 998244353;
////素因数分解 O(n**1/2)
map<int, int> prime_factor(int n){
map<int, int> res;
for(ll i = 2; i * i <= n; i++){
while(n % i == 0){
++res[i];
n /= i;
}
}
if(n != 1) res[n] = 1;
return res;
}
const int MAX_N = 400000;
ll fact[MAX_N+100];
ll frev[MAX_N+100];
ll rev(ll a, ll p){
if(p == 0) return 1;
ll tmp = rev(a, p/2);
tmp = tmp * tmp % MOD;
if(p % 2 == 1) tmp = tmp * a % MOD;
return tmp;
}
void calc_fact(){
fact[0] = frev[0] = 1;
for(int i = 1; i <= MAX_N; i++){
fact[i] = (fact[i-1] * i) % MOD;
frev[i] = rev(fact[i], MOD-2);
}
}
ll comb(int n, int k){
if(n < 0 || k < 0 || n < k) return 0;
if(n == 0 || k == 0) return 1;
return fact[n] * frev[k] % MOD * frev[n-k] % MOD;
}
ll perm(int n, int k){
if(n < 0 || k < 0 || n < k) return 0;
if(n == 0 || k == 0) return 1;
return fact[n] * frev[n-k] % MOD;
}
void prepare(){
fact[0] = frev[0] = 1;
for(int i = 1; i <= MAX_N; i++){
fact[i] = (fact[i-1] * i) % MOD;
frev[i] = rev(fact[i], MOD-2);
}
}
ll solve(ll n, ll k){
return comb(n+k-1, n);
}
int main(){
ll N, M;
cin >> N >> M;
//in main function
prepare();
ll ans = 0;
REPi(m, 1, M+1){
auto prime = prime_factor(m);
ll tmp = 1;
for(auto& [p, cnt] : prime){
//tmp *= solve(N-1, cnt);
//tmp *= solve(N-1, cnt+1);
tmp *= solve(cnt, N);
tmp %= MOD;
}
//printf("%d : %lld\n", m, tmp);
ans += tmp;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define int long long
#define rep(a,b,c) for(register int a=(b);a<=(c);++a)
#define dow(a,b,c) for(register int a=(b);a>=(c);--a)
using namespace std;
const int MaxN=200000+5;
int arr[MaxN],dif[MaxN],Sum[MaxN];
signed main()
{
register int n,q;
scanf("%lld%lld",&n,&q);
rep(i,1,n)
{
scanf("%lld",&arr[i]);
dif[i]=(arr[i]-arr[i-1])-1;
Sum[i]=(Sum[i-1]+dif[i]);
}
register int val,pos;
while(q--)
{
scanf("%lld",&val);
pos=(upper_bound(Sum+1,Sum+n+1,val-1)-(Sum+1));
printf("%lld\n",arr[pos]+(val-Sum[pos]));
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define dep(i, r, l) for (int i = r; i >= l; --i)
void read (int &x) {
x = 0; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - 48, c = getchar();
}
const int N = 2e6 + 10, mod = 1e9 + 7;
int n, m, k, fac[N], inv[N];
int C (int n, int m) {
if (n < m) return 0;
if (m == 0) return 1;
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int Pow (int a, int k) {
if (k == 1) return a;
int S = Pow (a, k >> 1);
if (k & 1) return S * S % mod * a % mod;
else return S * S % mod;
}
signed main () {
fac[0] = 1; rep(i, 1, N - 1) fac[i] = fac[i - 1] * i % mod;
inv[N - 1] = Pow(fac[N - 1], mod - 2);
dep(i, N - 2, 0) inv[i] = inv[i + 1] * (i + 1) % mod;
read(n), read(m), read(k);
if (m == 0 && k < n || m + k < n) puts("0");
else printf("%lld", (C(n + m, n) - C(n + m, m + k + 1) + mod) % mod);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int i,j;
while (cin>>i){
int arr[200]={0};
long ans = 0;
for (int a=0;a<i;a++){
cin>>j;
ans+=arr[j%200];
arr[j%200]+=1;
}
cout<<ans<<'\n';
}
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <climits>
#include <map>
#include <set>
#include <iomanip>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unordered_map>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#ifndef ONLINE_JUDGE
#define imie(x) cerr << #x << " = " << x << '\n';
#else
#define imie(x)
#endif
void debug(vector<int> v) {
cerr << "[";
int n = (int)v.size();
for(int i = 0; i < n; i++) {
cerr << v[i] << (i == n-1 ? "" : ", ");
}
cerr << "]" << '\n';
}
// const int INF = 1e5 + 5;
void test_case() {
int n;
cin >> n;
vector<int> a(n);
map<int, long long> freq;
for(int i = 0; i < n; i++) {
cin >> a[i];
freq[a[i] % 200]++;
}
long long cnt = 0;
for(auto x : freq) {
cnt += (long long)x.second * (x.second-1);
}
cout << cnt / 2LL << '\n';
}
/*
1. Multiset?
2. binary or two pointers
3. edgecases (can it be 0 or 1)
3. Upper_bound or Lower_bound???
Easy Problem = Easy Solution
*/
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("Error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int T;
T = 1;
while(T-- >0) {
test_case();
}
}
|
#include <iostream>
#define DENOM 998244353
using namespace std;
int main(){
long long a, b, c;
cin >> a >> b >> c;
cout << ((((a*(a+1)/2)%DENOM) * ((b*(b+1)/2)%DENOM))%DENOM * ((c*(c+1)/2)%DENOM))%DENOM << endl;
return 0;
} | #include <bits/stdc++.h>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << endl
#define dump_4(x1, x2, x3, x4) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
const long long MOD = 998244353;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
const ll LINF = 2e18;
const int INF = 1e9;
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 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, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
void solve(ll _A, ll _B, ll _C) {
mint c = _C;
mint b = _B;
mint a = _A;
mint p = (c * (c + 1)) / 2;
mint q = (b * (b + 1)) / 2;
mint r = (a * (a + 1)) / 2;
cout << p * q * r << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll A;
scanf("%lld", &A);
ll B;
scanf("%lld", &B);
ll C;
scanf("%lld", &C);
solve(A, B, C);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
void solve(){
long long x,y,a,b,tot=0;cin>>x>>y>>a>>b;
while(x<y){
if(1e18-b<=x){
while(x<y){
if(1e18/a<=x) break;
x*=a;
tot++;
}
break;
}
if(1e18/a<=x){
while(x<y){
if(1e18-b<=x) break;
x+=b;tot++;
}
break;
}
if(x*a>x+b) {
tot+=(y-x)/b;
x+=((y-x)/b)*b;
break;
}
else {
x*=a;tot++;
}
}
if(x >= y) tot--;
cout<<tot<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("bhar.txt", "w", stdout);
#endif
long long q(1);//cin>>q;
while(q--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define trace(...) debug(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void debug(const char* name, Arg1&& arg1){
cerr <<fixed<<setprecision(6)<< name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void debug(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " | ";
debug(comma + 1, args...);
}
#define mp(x,y) make_pair(x,y)
const int INF=1000000000+5;
const int N=100+5;
const int M=3e5+5;
const ll oo=1e18+5;
const ll mod=998244353;
const long double eps = 1e-9;
ll extended_gcd(ll a,ll b,ll& x,ll& y){
ll t;
if(b==0){
x=1,y=0;
return a;
}
ll d=extended_gcd(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
}
void solve(){
ll X,Y,P,Q;
cin>>X>>Y>>P>>Q;
ll ans=LLONG_MAX;
for(int y=0;y<Y;++y){
for(int q=0;q<Q;++q){
//~ n*(2*X+2*Y)+X+y=m*(P+Q)+P+q
//~ n*u-m*v=w
ll u=(2*X+2*Y);
ll v=(P+Q);
ll w=P+q-X-y;
ll s,t;
ll gcd=extended_gcd(u,v,s,t);
if(w%gcd) continue;
s*=w/gcd;
t*=w/gcd;
if(s>=0){
s%=v/gcd;
}
else{
s+=(-s+v/gcd-1)/(v/gcd)*(v/gcd);
}
ll cur=s*u+X+y;
ans=min(ans,cur);
}
}
//~ trace(ans);
if(ans==LLONG_MAX) cout<<"infinity";
else cout<<ans;
cout<<endl;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
//~ int s,t;
//~ int d=extended_gcd(4,6,s,t);
//~ trace(d);
//~ trace(s,t);
//~ trace(4*s+6*t);
int t;
cin>>t;
while(t--) solve();
return 0;
}
|
#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 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 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,m);
vector<ll> a(n),b(m);
in(a,b);
vector2d(int,dp,n+1,m+1,INF);
rep(i,n+1)dp[i][0]=i;
rep(i,m+1)dp[0][i]=i;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
chmin(dp[i][j],dp[i][j-1]+1);
chmin(dp[i][j],dp[i-1][j]+1);
if(a[i-1]==b[j-1]){
chmin(dp[i][j],dp[i-1][j-1]);
}else{
chmin(dp[i][j],dp[i-1][j-1]+1);
}
}
}
out(dp[n][m]);
} | #include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)
ll binpow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
int a[n*2],b[n*2],ans[n*2];
array<int,2> c[n*2];
for (int i=0;i<n*2;++i)
{
cin>>a[i];
c[i]={a[i],i};
}
sort(c,c+n*2);
for (int i=0;i<n;++i)
b[c[i][1]]=0;
for (int i=n;i<n*2;++i)
b[c[i][1]]=1;
stack<int> s;
for (int i=0;i<n*2;++i)
{
if (s.empty()||b[i]==b[s.top()])
s.push(i);
else
{
ans[s.top()]=1;
ans[i]=0;
s.pop();
}
}
for (int i=0;i<n*2;++i)
{
if (ans[i])
cout<<'(';
else
cout<<')';
}
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
int main() {
int h,w;
cin >> h >> w;
int min_ = INF;
vector<vector<int> > a(h,vector<int>(w));
for(int i = 0;i<h; i++){
for(int j = 0; j<w; j++){
cin >> a[i][j];
min_ = min(a[i][j],min_);
}
}
int ans = 0;
for(int i = 0;i<h; i++){
for(int j = 0; j<w; j++){
ans += (a[i][j]-min_);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
int A[N];
int B[M];
int dp[N][M];
for(int i = 0; i < N; i++) cin >> A[i];
for(int i = 0; i < M; i++) cin >> B[i];
if(A[0] == B[0]) dp[0][0] = 0;
else dp[0][0] = 1;
for(int i = 1; i < N; i++){
if(A[i] == B[0]) dp[i][0] = i;
else dp[i][0] = dp[i-1][0] + 1;
}
for(int i = 1; i < M; i++){
if(A[0] == B[i]) dp[0][i] = i;
else dp[0][i] = dp[0][i-1] + 1;
}
for(int i = 1; i < N; i++){
for(int j = 1; j < M; j++){
int tmp = min(dp[i-1][j] + 1, dp[i][j-1] + 1);
if(A[i] == B[j]) dp[i][j] = min(tmp, dp[i-1][j-1]);
else dp[i][j] = min(tmp, dp[i-1][j-1] + 1);
}
}
cout << dp[N-1][M-1] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define dup(x,y) (((x)+(y)-1)/(y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
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; }
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
ll mpow(ll x, ll p) {
if (p==0) return 1;
ll res=mpow(x,p/2); res=res*res%MOD;
return p%2?res*x%MOD:res;
}
ll lpow(ll x, ll p) {
if (p==0) return 1;
ll res=lpow(x,p/2); res*=res;
return p%2?res*x:res;
}
#ifndef fenwick_tree_hpp
#define fenwick_tree_hpp
#include<vector>
template<typename T> struct Fenwick_Tree {
private:
int n;
std::vector<T> bit;
public:
Fenwick_Tree(int _n): n(_n), bit(_n, 0) {}
// [0, r)の要素の総和
T sum(int i) {
T s = 0;
while (i > 0) {
s += bit[i-1];
i -= i & -i;
}
return s;
}
// [l, r)の要素の総和
T sum(int l, int r) {
return sum(r) - sum(l);
}
void add(int i, T x) {
i += 1;
while (i <= n) {
bit[i-1] += x;
i += i & -i;
}
}
};
#endif // fenwick_tree_hpp
const int M = 100000;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> batsu(n+1, 0);
Fenwick_Tree<double> fw_a(M+M+5), fw_b(M+M+5);
rep(i,k) {
int x; cin >> x;
batsu[x]++;
fw_a.add(x, 1);
}
for (int i = n-1; i >= 0; --i) {
if (batsu[i]) continue;
fw_a.add(i, fw_a.sum(i+1, i+m+1)/m);
fw_b.add(i, fw_b.sum(i+1, i+m+1)/m + 1);
}
double a = fw_a.sum(0, 1);
double b = fw_b.sum(0, 1);
if (1 - a == 0) cout << -1 << endl;
else {
double ans = b / (1 - a);
cout << fixed << setprecision(10) << ans << endl;
}
} | #include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;
using VI = vector <int>;
using VVI = vector <VI>;
using VLL = vector <long long>;
int main() {
int K;
cin >> K;
string S;
string T;
cin >> S >> T;
double winning = 0;
VLL remain(10, K);
VVI cnt(2, VI(10));
for (int i = 0; i < 4; ++i) {
--remain[S[i]-'0'];
--remain[T[i]-'0'];
++cnt[0][S[i]-'0'];
++cnt[1][T[i]-'0'];
}
long long totCard = 9*K-8;
double sumProb = 0;
for (int a = 1; a <= 9; ++a) {
for (int b = 1; b <= 9; ++b) {
double prob = 0;
VVI ncnt = cnt;
++ncnt[0][a];
++ncnt[1][b];
if (a==b) {
if (remain[a] < 2) {
continue;
}
prob = (double)remain[a]*(remain[b]-1)/(totCard*(totCard-1));
} else {
if (remain[a] < 1 || remain[b] < 1) {
continue;
}
prob = (double)remain[a]*remain[b]/(totCard*(totCard-1));
}
VLL score(2);
for (int i = 0; i < 2; ++i) {
for (int j = 1; j <= 9; ++j) {
long long base = j;
for (int k = 0; k < ncnt[i][j]; ++k) {
base *= 10;
}
score[i] += base;
}
}
// if T wins
// cout << "GO " << a << " " << b << " " << score[0] << " " << score[1] << endl;
if (score[0] > score[1]) {
// cout << "NOW " << a << " " << b << " " << prob << endl;
winning += prob;
}
sumProb += prob;
}
}
cout << setprecision(15) << fixed << winning << endl;
// cout << setprecision(15) << fixed << sumProb << endl;
return 0;
} |
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
typedef long long LL;
const int N = 505;
const LL MOD = 998244353;
int a[N][N];
inline int read() {
char ch = getchar();
while (ch != 'B' && ch != 'R' && ch != '.') ch = getchar();
if (ch == 'B') return 0;
if (ch == 'R') return 1;
return 2;
}
void solve() {
int n, m;
std::cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = read();
}
}
int ans = 0;
for (int i = 1; i <= n + m - 1; i++) {
int tt[] = {0, 0, 0};
for (int j = i; j >= 1; j--) {
if (j > n) continue;
int t = i - j + 1;
if (t > m) continue;
tt[a[j][t]]++;
}
if (tt[0] != 0 && tt[1] != 0) {
puts("0");
return;
} else if (tt[0] == 0 && tt[1] == 0) {
ans ++;
}
}
LL res = 1;
for (int i = 0; i < ans; i++) {
res = res * 2 % MOD;
}
std::cout << res << std::endl;
}
int main() {
solve();
return 0;
}
/*
2 4
..B.
....
*/ | #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) - 1, i##end = (b); i >= i##end; --i)
#define REP(i, a) rep(i, 0, a)
#define PER(i, a) per(i, a, 0)
namespace IO {
const int MAXIOSIZE = 1 << 24 | 1;
unsigned char buf[MAXIOSIZE], *p1, *p2;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)
template <typename T> void read(T& x) {
x = 0; char ch = gc; bool flg = false;
for (; ch < '0' || '9' < ch; ch = gc) if (ch == '-') flg |= true;
for (; '0' <= ch && ch <= '9'; ch = gc) x = x * 10 + ch - '0';
flg ? x = -x : 0;
}
template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); }
template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); }
}
const int MAXN = 2e5 + 10;
const int P = 998244353;
int qpower(int a, int x) {
int ret = 1;
for (; x; x >>= 1, a = 1ll * a * a % P) x & 1 ? ret = 1ll * ret * a % P : 0;
return ret;
}
int n, E[MAXN], cnt, vis[MAXN];
void work(int st, int idx) {
if (vis[st]) return ;
int cur = st;
while (1) {
vis[cur] = idx;
cur = E[cur];
if (vis[cur]) {
cnt += vis[cur] == idx;
break;
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
IO::read(n);
REP(i, n) IO::read(E[i]), E[i]--;
REP(i, n) work(i, i + 1);
IO::write((qpower(2, cnt) - 1 + P) % P);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,w;
cin>>n>>w;
cout<<n/w;
}
| // Author:- Pratik Kinger
// Birla Institute of Technology
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define all(a) (a).begin(), (a).end()
#define fi first
#define si second
#define sz(x) (ll) x.size()
#define endl '\n'
#define mod 1000000007
#define mset(m, v) memset(m, v, sizeof(m))
using namespace std;
const ll MOD = 1000000000 + 7;
ll powermod(ll a, ll b, ll modi)
{
a %= modi;
ll res = 1;
while (b)
{
if (b % 2)
{
res = (res * a) % modi;
}
b /= 2;
a = (a * a) % modi;
}
return res;
}
ll power(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll binarySearch(ll a[], ll low, ll high, ll key)
{
while (low <= high)
{
ll mid = (low + high) / 2;
if (a[mid] < key)
{
low = mid + 1;
}
else if (a[mid] > key)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
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 binarytodecimal(string n)
{
string num = n;
ll dec_value = 0;
ll base = 1;
ll len = num.length();
for (ll i = len - 1; i >= 0; i--)
{
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
string dtb(ll n)
{
string s = "";
while (n > 0)
{
if (n % 2 == 0)
s.pb('0');
else
s.pb('1');
n /= 2;
}
reverse(all(s));
return s;
}
vi seive;
void Seive()
{
const ll maxn = 1e6 + 5;
seive.resize(maxn);
f(i, 0, maxn) seive[i] = i;
for (ll i = 2; i <= maxn; i += 2)
seive[i] = 2;
seive[1] = -1;
seive[0] = -1;
for (ll i = 3; i <= maxn; i += 2)
if (i == seive[i])
for (ll j = i * i; j < maxn; j += i)
if (seive[j] == j)
seive[j] = i;
}
//------------------------------CODE--HERE--------------------------
//----------global--variables-------------------
//------------end-------------------------------
void solve()
{
ll n,w;
cin>>n>>w;
cout<<floor(n/w)<<endl;
}
//------------------------------
int main()
{
//FAST INPUT OUTPUT
FIO;
//INPUT OUTPUT
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t = 1;
while (t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using namespace std;
int main() {
int N, K, M;
cin >> N >> K >> M;
vector<int> A(N-1);
int sum = 0;
rep(i, N-1) {
cin >> A[i];
sum += A[i];
}
for (int i=0; i<=K; ++i) {
double s = sum + i;
if (s/N >= M) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N,K,M; cin >> N >> K >> M;
ll sum = 0;
for (int i = 0;i < N-1;i++){
ll a; cin >> a;
sum += a;
}
ll goal = M*N;
if(goal <= sum) cout << 0 << endl;
else if(goal-sum > K) cout << -1 << endl;
else cout << goal-sum << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin >> n;
n = min(n, 8);
vector<int> a(n);
for (auto& ai : a) {
cin >> ai;
}
vector<int> f(200, -1);
for (int mask = 1; mask < (1 << n); ++mask) {
int64_t sum = 0;
for (int bit = 0; bit < n; ++bit) {
if (mask & (1 << bit)) {
sum += a[bit];
}
}
sum %= 200;
if (f[sum] != -1) {
auto print_mask = [&] (int mask) -> void {
cout << __builtin_popcount(mask) << " ";
for (int bit = 0; bit < n; ++bit) {
if (mask & (1 << bit)) {
cout << bit + 1 << " ";
}
}
cout << "\n";
};
cout << "Yes\n";
print_mask(f[sum]);
print_mask(mask);
return 0;
} else {
f[sum] = mask;
}
}
cout << "No\n";
return 0;
} | #include <bits/stdc++.h>
#include <unordered_set>
#include <cmath>
#include <algorithm>
// URL:
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n)? "YES": "NO") << endl
#define Yes(n) cout << ((n)? "Yes": "No") << endl
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fore(x, a) for(auto& (x) : (a))
#define FORL(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define repll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define VECCIN(x) for(auto& youso_: (x)) cin >> youso_
#define VECCOUT(x) for(auto& youso_: (x)) cout << youso_ << " ";cout << endl
#define pb push_back
#define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
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; }
const ll INFL = 1e18;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int main(){
ll T;
cin >> T;
repll(i,T) {
ll N;
cin >> N;
if (N % 2 == 1) {
cout << "Odd" << endl;
}
else {
N /= 2;
if (N % 2 == 1) {
cout << "Same" << endl;
}
else {
cout << "Even" << endl;
}
}
}
} |
#include<bits/stdc++.h>
using namespace std;
int main() {
long n;
cin>>n;
set<long> ans;
for(long d=1;d*d<=n;d++) {
if(n%d==0) {
ans.insert(d);
ans.insert(n/d);
}
}
for(auto x:ans) cout<<x<<endl;
} | #include<bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, a, b) for (ll i = (a); i < (b); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using tl = tuple<ll, ll, ll>;
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
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; }
ll INF = 1001001001;
ll LINF = 1001001001001001001ll;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n; cin>>n;
map<ll, ll> mp;
rrep(i, 2, n + 1) {
ll temp = i;
for (ll j = 2; j * j <= temp; ++j) {
ll cur = 0;
while (temp % j == 0) {
cur++;
temp /= j;
}
chmax(mp[j], cur);
}
chmax(mp[temp], 1ll);
}
ll ans = 1;
for (auto p : mp) {
rep(i, p.se) ans *= p.fi;
}
cout<<ans + 1<<endl;
} |
#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 10000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
ld d[105][105];
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
ll n; cin >> n;
vector<ll> x(n + 5), y(n + 5);
rep(i, n) {
cin >> x[i] >> y[i];
}
if(n==1){
cout<<max(100-y[0],100+y[0])/2<<endl;
return 0;
}
//行けないか
vector<vector<ll> > G(123456);
rep(i, n) {
rep(j, n) {
if (i != j) { G[i].push_back(j); }
d[i][j] = sqrt(pow(abs(x[i] - x[j]), 2) + pow(abs(y[i] - y[j]), 2));
}
}
rep(i, n) {
G[i].push_back(n);
d[i][n] = y[i] + 100;
}
//cout << 222 << endl;
ld ans = 200;
rep(aa, n) {
queue<ll> que; queue<ld> cnt;
vector<ld> visit(n + 1, INF); visit[aa] = 100 - y[aa];
for (int i = 0; i < G[aa].size(); i++) {
if (G[aa][i] == n) { ans = min(ans, max(visit[aa], d[aa][G[aa][i]])); }
que.push(G[aa][i]); cnt.push(max(visit[aa], d[aa][G[aa][i]]));
visit[G[aa][i]] = max(visit[aa], d[aa][G[aa][i]]);
}
while (que.size()) {
//cout << visit[que.front()] << endl;
for (int i = 0; i < G[que.front()].size(); i++) {
ld uio = 0;
if (G[que.front()][i] == n) {
uio = max(cnt.front(), d[que.front()][G[que.front()][i]]);
ans = min(ans, uio); continue;
}
uio = max(cnt.front(), d[que.front()][G[que.front()][i]]);
if (visit[G[que.front()][i]] > uio) {
que.push(G[que.front()][i]);
cnt.push(uio); visit[G[que.front()][i]] = uio;
}
}
que.pop(); cnt.pop();
}
}
ans /= 2;
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif
#define mp make_pair
#define pb push_back
#define sz(x) (int)((x).size())
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define REP(x, n) for(int x = 0; x < n; x++)
#define REPV(x, v, n) for(int x = v; x < n; x++)
#define REVE(x, n) for(int x = n; x >= 0; x--)
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;
class UnionFind
{
int N, cnt;
vector<int> parent, size;
public:
UnionFind() { N = 0; }
UnionFind(int N) :N(N) { clear(); }
void clear() { cnt = N; size.assign(N + 1, 1); parent.assign(N + 1, 0); for(int i = 1; i <= N; i++) parent[i] = i; }
void reset(int sz) { N = sz; clear(); }
inline int count() const { assert(N != 0); return cnt; }
int getSetSize(int i) { assert(N != 0 && i >= 1 && i <= N); return size[findSet(i)]; }
int findSet(int i) { assert(N != 0 && i >= 1 && i <= N); return (parent[i] == i) ? i : (parent[i] = findSet(parent[i])); }
bool isSameSet(int i, int j) { assert(N != 0 && i >= 1 && i <= N && j >= 1 && j <= N); return findSet(i) == findSet(j); }
void unite(int i, int j)
{
assert(N != 0 && i >= 1 && i <= N && j >= 1 && j <= N);
int x = findSet(i), y = findSet(j);
if(x == y)
return;
if(size[x] < size[y])
swap(x, y);
parent[y] = x;
size[x] += size[y];
cnt--;
}
};
const ld eps = 1e-7;
int N;
pll nails[111];
bool can(ld R)
{
UnionFind uf(N + 2);
int up = N + 1, down = N + 2;
REPV(i, 1, N + 1)
{
if(100 - nails[i].second <= 2 * R) uf.unite(i, up);
if(100 + nails[i].second <= 2 * R) uf.unite(i, down);
}
REPV(i, 1, N + 1)
REPV(j, i + 1, N + 1)
if(pow(nails[i].first - nails[j].first, 2) + pow(nails[i].second - nails[j].second, 2) <= 4 * R * R)
uf.unite(i, j);
return !uf.isSameSet(up, down);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
REPV(i, 1, N + 1) cin >> nails[i].first >> nails[i].second;
ld lo = 0.0, hi = 500.0;
while(hi - lo > eps)
{
ld mid = (hi + lo) / 2.0;
if(can(mid)) lo = mid;
else hi = mid;
}
cout.precision(9);
cout << fixed << lo << "\n";
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
ll N;
cin>>N;
vector<pair<ll,char> > vp(2*N);
ll n;
char c;
map<char,ll> mp;
for(int i=0;i<2*N;i++){
cin>>n;
cin>>c;
vp[i]=make_pair(n,c);
mp[c]++;
}
vector<pair<char,int> > vcc;
vector<pair<char,int> > vce;
for(auto a:mp){
//cout<<a.first<<" "<<a.second<<endl;
//a.second=(a.second)%2;
// cout<<a.first<<" "<<a.second<<endl;
if(a.second%2==1){
vcc.push_back(make_pair(a.first,0));
}else{
vce.push_back(make_pair(a.first,0));
}
}
/*
for(auto a:vcc){
cout<<a.first<<" "<<a.second;
}
for(auto a:vce){
cout<<a.first<<" "<<a.second;
}
*/
sort(vp.begin(),vp.end(),[](pair<ll,char> a,pair<ll,char> b){
return a.first<b.first;
});
/*
for(auto a: mp){
cout<<a.first<<" "<<a.second<<endl;
}
*/
vector<pair<ll,pair<char,char> > > vc;
for(int i=0;i<2*N-1;i++){
vc.push_back(pair<ll,pair<char,char> >(abs(vp[i].first-vp[i+1].first),pair<char,char>(vp[i].second,vp[i+1].second)));
}
sort(vc.begin(),vc.end(),[](pair<ll,pair<char,char> > a,pair<ll,pair<char,char> > b){
return a.first<b.first;
});
ll ans=0;
ll ans1=0;
ll ans2=0;
ll cnt=0;
bool flag1=false;
bool flag2=false;
//cout<<mp['R']<<" "<<mp['B']<<" "<<mp['C']<<endl;
if(mp['R']%2==0&&mp['B']%2==0&&mp['G']%2==0){
ans=0;
}else{
// cout<<"---"<<endl;
for(int i=0;i<2*N-1;i++){
if(((vc[i].second.first==vcc[0].first)&&(vc[i].second.second==vcc[1].first))||(vc[i].second.first==vcc[1].first&&vc[i].second.second==vcc[0].first)){
//cout<<vc[i].first<<endl;
if(cnt==0){
// cout<<"0000"<<endl;
ans1+=vc[i].first;
flag1=true;
}
cnt++;
}else{
if(((vc[i].second.first==vce[0].first)&&(vc[i].second.second==vcc[0].first))||(vc[i].second.first==vcc[0].first&&vc[i].second.second==vce[0].first)){
if(vcc[0].second==0){
ans2+=vc[i].first;
}
vcc[0].second++;
}else if(((vc[i].second.first==vce[0].first)&&(vc[i].second.second==vcc[1].first))||(vc[i].second.first==vcc[1].first&&vc[i].second.second==vce[0].first)){
if(vcc[1].second==0){
ans2+=vc[i].first;
}
vcc[1].second++;
}
}
}
if(vcc[0].second>0&&vcc[1].second>0){
flag2=true;
}
if(flag1==true&&flag2==false){
ans=ans1;
}else if(flag1==false&&flag2==true){
ans=ans2;
}else{
ans=min(ans1,ans2);
}
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
ll id(char c) {
if (c=='R') return 0;
if (c=='G') return 1;
return 2;
}
bool is_end(vector<vector<ll>> vec) {
for (auto a : vec) {
if (a.size()%2) return false;
}
return true;
}
int main() {
ll N; cin >> N;
vector<vector<ll>> a(3);
rep(i,2*N) {
ll x; char c;
cin >> x >> c;
a[id(c)].push_back(x);
}
ll cnt0 = 0;
rep(i,3) if (a[i].size()==0) cnt0++;
if (cnt0==2) Cout(0);
if (is_end(a)) Cout(0);
rep(i,2) if (a[i].size()%2==0) swap(a[i],a[2]);
rep(i,3) sort(all(a[i]));
ll ans = inf;
rep(i,a[0].size()) {
ll x = a[0][i];
ll l = -1, r = a[1].size();
ll res = inf;
while (r-l>1) {
ll m = (l+r)/2;
if (a[1][m]<=x) l = m;
else r = m;
}
if (r<a[1].size()) chmin(res,abs(a[1][r]-x));
if (l>=0) chmin(res,abs(a[1][l]-x));
chmin(ans,res);
if (ans==0) Cout(0);
}
if (cnt0==1) Cout(ans);
vector<ll> X, Y;
rep(i,a[2].size()) {
ll x = a[2][i];
rep(j,2) {
ll l = -1, r = a[j].size();
ll res = inf;
while (r-l>1) {
ll m = (l+r)/2;
if (a[j][m]<=x) l = m;
else r = m;
}
if (r<a[j].size()) chmin(res,abs(a[j][r]-x));
if (l>=0) chmin(res,abs(a[j][l]-x));
if (j%2) X.push_back(res);
else Y.push_back(res);
}
}
sort(all(X)); sort(all(Y));
chmin(ans,X[0]+Y[0]);
Cout(ans);
} |
#include <bits/stdc++.h>
#define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++)
#define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++)
#define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--)
#define deb(x) std::cout << #x << " " << x << endl;
#define debl(x) std::cout << #x << " " << x << " ";
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
namespace std{
template<class Fun>
class y_combinator_result{
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){}
template<class ...Args>
decltype(auto) operator()(Args&&...args){
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun && fun){
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
};
template<typename T>
bool umax(T& a, T b){
bool ret = a < b;
if(ret) a = b;
return ret;
}
template<typename T>
bool umin(T& a, T b){
bool ret = a > b;
if(ret) a = b;
return ret;
}
struct edge{
int to; ll cost;
int from;
edge(){ edge(0,0);}
edge(int to_, ll cost_) : to(to_), cost(cost_){}
edge(int to_, int from_, ll cost_) : to(to_), cost(cost_), from(from_){}
};
template<typename... T>
void read(T& ... a){
((cin >> a),...);
}
template<typename... T>
void write(T... a){
((cout << a),...);
}
template<typename T>
vector<T> read_array(int sz){
vector<T> ret(sz);
for(auto & x : ret) cin >> x;
return ret;
}
void solve(){
int n;
read(n);
ll ans = 2e9;
REP(i, n){
ll a, p, x;
read(a, p, x);
if(a < x){
umin(ans, p);
}
}
if(ans > (ll) 1e9) ans = -1;
write(ans,"\n");
}
int main()
{
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
int T = 1;
// cin >> T;
while(T--)
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main( )
{
ll n,a,b,c;
cin>>n;
ll ans = INT_MAX,cnt=0;
while(n--)
{
cin>>a>>b>>c;
if(c-a > 0){ans=min(ans,b);++cnt;}
}
if(cnt)cout<<ans<<endl;
else cout<<-1<<endl;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
const int kMaxN = 2e5 + 1;
const int kM = 1e9 + 7;
const int kA = 16;
string s;
long long f[kMaxN][kA + 1], g[kMaxN][kA + 1];
int k, n, t, ans, c[kA];
void C1() {
f[1][1] = kA - 1;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= k; j++) {
f[i][j] = (f[i][j] + f[i - 1][j] * j + f[i - 1][j - 1] * (17 - j)) % kM;
}
ans = (ans + f[i][k]) % kM;
}
}
void C2() {
g[1][1] = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
g[i][j] = (g[i][j] + g[i - 1][j] * j + g[i - 1][j - 1] * (17 - j)) % kM;
}
for (int j = 0; j < s[i - 1]; j++) {
t += !c[j]++;
g[i][t] = (g[i][t] + 1) % kM;
t -= !--c[j];
}
t += !c[s[i - 1]]++;
}
ans = (ans + g[n][k] + (t == k)) % kM;
}
int main() {
cin >> s >> k;
n = s.length();
for (int i = 0; i < n; i++) {
s[i] -= (s[i] <= '9' ? '0' : 'A' - 10);
}
C1(), C2();
cout << ans;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxl=2e5+10;
const int mod=1e9+7;
int n,k;
int a[maxl];
ll dp[maxl][17][2][2];
char s[maxl];
inline ll dfs(int pos,int st,bool up,bool lead)
{
int cnt=__builtin_popcount(st);
if(cnt>k) return 0;
if(pos>n) return cnt==k;
ll &ret=dp[pos][cnt][up][lead];
if(ret!=-1)
return ret;
ret=0;
int r=up?a[pos]:15;
for(int i=0;i<=r;i++)
ret=(ret+dfs(pos+1,(!i&&!lead)?st:st|(1<<i),up&&(i==r),lead|i))%mod;
return ret;
}
int main()
{
scanf("%s",s+1);scanf("%d",&k);
n=strlen(s+1);
for(int i=1;i<=n;i++)
if(s[i]>='A' && s[i]<='Z')
a[i]=s[i]-'A'+10;
else
a[i]=s[i]-'0';
memset(dp,-1,sizeof(dp));
printf("%lld\n",dfs(1,0,1,0));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 3005, p = 1e9 + 7;
int n, trans[Maxn][Maxn], maxi[Maxn][Maxn];
long long ans, a[Maxn], sum[Maxn], f[Maxn][Maxn];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]), sum[i] = sum[i - 1] + a[i];
for (int j = 1; j <= n; j++)
{
trans[i][j] = maxi[j][sum[i] % j];
maxi[j][sum[i] % j] = i;
}
}
f[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (trans[i][j] != -1)
(f[i][j] += f[trans[i][j]][j] + f[trans[i][j]][j - 1]) %= p;
for (int i = 1; i <= n; i++)
(ans += f[n][i]) %= p;
printf("%lld", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
return ((T(1) << idx) & b) != 0;
}
template<long long M, typename T=long long>
struct modint {
modint(T v=T(0)) : val((v >= 0 ? v : (M - ((-v) % M))) % M) {}
using this_type = modint<M, T>;
T val;
this_type operator++(int) {
this_type ret = *this;
val++; val %= M;
return ret;
}
this_type operator--(int) {
this_type ret = *this;
val += M-1; val %= M;
return ret;
}
this_type &operator++() {
val++; val %= M;
return *this;
}
this_type &operator--() {
val += M-1; val %= M;
return *this;
}
this_type operator+() const { return *this; }
this_type operator-() const { return this_type(M-val); };
friend this_type operator+(const this_type &lhs, const this_type &rhs) {
return this_type(lhs) += rhs;
}
friend this_type operator-(const this_type &lhs, const this_type &rhs) {
return this_type(lhs) -= rhs;
}
friend this_type operator*(const this_type &lhs, const this_type &rhs) {
return this_type(lhs) *= rhs;
}
friend this_type operator/(const this_type &lhs, const this_type &rhs) {
return this_type(lhs) /= rhs;
}
this_type pow(long long b) const {
this_type ret = 1, a = *this;
while(b != 0) {
if(b % 2 != 0) {
ret *= a;
}
b /= 2;
a = a * a;
}
return ret;
}
this_type inv() const {
return pow(M-2);
}
this_type& operator+=(const this_type &rhs) {
val += rhs.val; val %= M; return *this;
}
this_type& operator-=(const this_type &rhs) {
val += M - rhs.val; val %= M; return *this;
}
this_type& operator*=(const this_type &rhs) {
val *= rhs.val; val %= M; return *this;
}
this_type& operator/=(const this_type &rhs) {
*this *= rhs.inv(); return *this;
}
friend bool operator==(const this_type &lhs, const this_type &rhs) {
return lhs.val == rhs.val;
}
friend bool operator!=(const this_type &lhs, const this_type &rhs) {
return lhs.val != rhs.val;
}
T mod() const {return M;}
};
using mi = modint<1000000007>;
using vmi = vector<mi>;
using vvmi = vector<vmi>;
int main(int argc, char *argv[]) {
i64 i, j, k;
i64 N; RI(N);
vi A(N); RVI(A);
vvmi ans(N+1, vmi(N+1, 0));
fill(all(ans[1]), 1);
ans[1][0] = 0;
REP(2, N+1, k) {
vi prev(N+1, -1);
i64 S = 0;
vi res(k, -1);
res[0] = 0;
REP(1, N+1, i) {
S += A[i-1];
S %= k;
prev[i] = res[S];
res[S] = i;
}
REP(1, N+1, i) {
ans[k][i] = 0;
if(prev[i] != -1) {
ans[k][i] += ans[k][prev[i]];
ans[k][i] += ans[k-1][prev[i]];
}
}
}
mi a = 0;
REP(1, N+1, k) {
a += ans[k].back();
}
WI(a.val);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(nullptr); // Do not flush "cout" when processing "cin".
int n, k;
cin >> n >> k;
vector<vector<int>> t(8, vector<int>(8));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> t.at(i).at(j);
}
}
// 都市番号は 0-indexed とする。
// 都市番号が 1 〜 n-1 の都市について、全ての順列を試す。
//
// p の要素番号は 0 〜 n-2 となる。
vector<int> p;
for (int i = 1; i < n; i++) {
p.push_back(i);
}
int ans = 0;
do {
// 都市 0 から都市 p[0] までの移動距離
int total = t.at(0).at(p.at(0));
// 都市 p[0] から都市 p[1] への移動時間
// ...
// 都市 p[n-3] から都市 p[n-2] への移動時間
//
// 上記の移動時間をそれぞれ加算する。
for (int i = 0; i < n - 2; i++) {
int from = p.at(i);
int to = p.at(i + 1);
total += t.at(from).at(to);
}
// 都市 p[n-2] から都市 0 への移動時間
total += t.at(p.at(n - 2)).at(0);
if (total == k) {
ans++;
}
// ### next_permutation
//
// 今回の場合、vector コンテナクラスのメンバ変数である
// vector<int>::iterator 型の値を引数に取る。
//
// [first, last) で指定した区間の要素を順列に並べ替える。
// そのため、next_permutation の第一引数を p.begin() + 1
// とする場合、0 番目の要素は並べ替えられない。
//
// また、next_permutation は次の順列を生成することができる場合、
// true、生成できない場合は false を返す。
//
// next_permutation は辞書順に並び替えて順列を生成する。
// そのため、以下の例では {1, 2, 3}, {1, 3, 2} の組み合わせは生成されない。
// {2, 1, 3} の次は、辞書順で {2, 3, 1} が生成される。
//
// vector<int> v = {2, 1, 3};
// do {
// ...
// } while (next_permutation(v.begin(), v.end()));
//
// 次の順列が生成できなくなった時点で vector コンテナは
// 辞書順の最後の並びとなっている。
//
// 上記 v = {2, 1, 3} の例では、
// v = {3, 2, 1} に並び替えられている。
//
// おそらく、順列が生成できず false を返すタイミングで、
// vector コンテナの要素を reverse(v.begin(), v.end()) で
// 辞書順の最初の並びにしている。
//
// 上記例では v = {3, 2, 1} を
// v = {1, 2, 3} にしている。
//
// これより do-while の実行前に vector の要素を予め
// sort している場合は、do-while 終了後に vector の値が
// 元に戻っている(あるいは、do-while ブロック内でのみ
// 順番が変更されている)ように見える。
//
} while (next_permutation(p.begin(), p.end()));
cout << ans << endl;
return 0;
}
| const bool isDebugMode = true;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const int SHOWSIZE = 1 << 5;
const long long MOD = 1000000007; //998244353;
const long long dy[] = {0, 0, 1, 1};
const long long dx[] = {0, 1, 0, 1};
const string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
long long power(long long a, long long n){long long res = 1; while(n > 0){if(n & 1)res = res * a; a = a * a; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, min(v.size(), SHOWSIZE)) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, min(v.size(), SHOWSIZE)){rep(j, min(v[i].size(), SHOWSIZE)) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b, a % b) : b;}
template<typename T> vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); }
template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v){for (auto &e : u) fill_v<T>(e, v...);}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }
void solve();
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// cin >> testcase;
while(testcase--) solve();
return 0;
}
void solve(){
ll H, W; cin >> H >> W;
vector<string> s(H);
rep(i, H) cin >> s[i];
ll ans = 0;
rep(i, H - 1)rep(j, W - 1){
ll cnt = 0;
rep(k, 4){
if(valid(H, W, i + dx[k], j + dy[k]) && s[i + dx[k]][j + dy[k]] == '.') cnt++;
}
if(cnt % 2) ans++;
}
pt(ans);
} |
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<x<<endl
using namespace std;
using ll = long long;
const ll MOD = 1e9+7; // 998244353
const ll LINF = LLONG_MAX; // ~ 10^18 ~ 1<<60
const int INF = INT_MAX;
const double PI = acos(-1);
int main(){
ll n, m; cin >> n >> m;
vector<ll> h(n);
vector<ll> w(m);
rep(i,0,n){
cin >> h[i];
}
rep(i,0,m){
cin >> w[i];
}
sort(all(h));
vector<ll> L(n, 0);
vector<ll> R(n, 0);
for(int i = 0; i <= n - 3; i += 2){
ll x = abs(h[i] - h[i + 1]);
L[i] = x;
L[i + 1] = x;
if(i != 0){
L[i] += L[i - 1];
L[i + 1] += L[i - 1];
}
}
for(int i = n - 1; i >= 1; i -= 2){
ll x = abs(h[i] - h[i - 1]);
R[i] = x;
R[i - 1] = x;
if(i != n - 1){
R[i] += R[i + 1];
R[i - 1] += R[i + 1];
}
}
ll ans = LINF;
for(int i = 0; i < m; i++){
ll it = lower_bound(all(h), w[i]) - h.begin();
ll temp = 0;
if(it % 2 == 1){
temp = abs(w[i] - h[it - 1]);
if(it - 2 >= 0) temp += L[it - 2];
if(it + 1 < n) temp += R[it + 1];
}else{
temp = abs(w[i] - h[it]);
if(it - 1 >= 0) temp += L[it - 1];
if(it + 1 < n) temp += R[it + 1];
}
ans = min(ans, temp);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lowbit(x) (x & (-x))
const ll inf = 1e17+9;
const ll mod = 1e9+7;
const ll maxn = 2e5+8;
int n,m;
int h[maxn],w[maxn];
ll L[maxn],R[maxn];
ll ans=1e18;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;++i) cin>>h[i];
for(int i=1;i<=m;++i) cin>>w[i];
sort(h+1,h+1+n);
for(int i=2;i<=n;i+=2) L[i]=L[i-2]+abs(h[i]-h[i-1]);
for(int i=n-1;i>=1;i-=2) R[i]=R[i+2]+abs(h[i]-h[i+1]);
for(int i=1;i<=m;++i){
int pos=lower_bound(h+1,h+1+n,w[i])-h;
if(pos%2==0) pos--;
ans=min(ans,L[pos-1]+R[pos+1]+abs(h[pos]-w[i]));
}
cout<<ans<<endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
int dx[4] = { 1, 0, -1,0 }, dy[4] = { 0, 1, 0, -1 };
int si, sj;
int N = 50;
int t[50][50];
int p[50][50];
map<pair<int, int>, pair<int, int>>mp;
bool seen[50][50];
int f(string s) {
int ans = p[si][sj];
int n = s.size();
for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)seen[i][j] = false;
seen[si][sj] = true;
int nowi = si, nowj = sj;
for (int i = 0; i < n; i++) {
char now = s[i];
int ni = nowi, nj = nowj;
int c = 0;
if (now == 'D') {
c = 0;
}
else if (now == 'R') {
c = 1;
}
else if (now == 'U') {
c = 2;
}
else {
c = 3;
}
ni += dx[c];
nj += dy[c];
if (ni < 0 || ni >= N || nj < 0 || nj >= N)return 0;
//if (t[ni][nj] == t[nowi][nowj] || seen[mp[{ni, nj}].first][mp[{ni, nj}].second])return 0;
if (seen[ni][nj])return 0;
seen[ni][nj] = true;
ans += p[ni][nj];
nowi = ni, nowj = nj;
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
clock_t start = clock();
cin >> si >> sj;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> t[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> p[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mp[{i, j}] = { i, j };
for (int s = 0; s < 4; s++) {
int ni = i + dx[s], nj = j + dy[s];
if (0 <= ni && ni < N && 0 <= nj && nj < N && t[ni][nj] == t[i][j]) {
mp[{i, j}] = { ni, nj };
}
}
}
}
string ans = "";
int nowscore = 0;
clock_t end = clock();
int cnt = 0;
while ((double)(end - start) / CLOCKS_PER_SEC < 0.98) {
end = clock();
cnt++;
queue<pair<int, int>>q;
vector<vector<int>>dist(N, vector<int>(N, -1));
q.push({ si, sj });
dist[si][sj] = 0;
string s = "";
while (!q.empty()) {
pair<int, int> pa = q.front(); q.pop();
vector<int>v;
for (int s = 0; s < 4; s++) {
int ni = pa.first + dx[s], nj = pa.second + dy[s];
if (0 <= ni && ni < N && 0 <= nj && nj < N && dist[ni][nj] == -1) {
if (t[ni][nj] != t[pa.first][pa.second] && dist[mp[{ni, nj}].first][mp[{ni, nj}].second] == -1) {
v.push_back(s);
}
}
}
int n = v.size();
if (n == 0)break;
int idx = v[rand() % n];
if (idx == 2)s += 'U';
else if (idx == 0)s += 'D';
else if (idx == 1)s += 'R';
else s += 'L';
int ni = pa.first + dx[idx], nj = pa.second + dy[idx];
if (0 <= ni && ni < N && 0 <= nj && nj < N && dist[ni][nj] == -1) {
if (t[ni][nj] != t[pa.first][pa.second]) {
q.push({ ni, nj });
dist[ni][nj] = dist[pa.first][pa.second] + 1;
}
}
}
int tscore = f(s);
if (nowscore < tscore) {
nowscore = tscore;
ans = s;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
//#include "atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> P;
constexpr ll inf=1ll<<61;
constexpr ll mod=998244353;
int a[1005],b[1005];
ll dp[1005][1005];
int main(){
int n,m;cin>>n>>m;
rep(i,n)cin>>a[i];
rep(i,m)cin>>b[i];
rep(i,n+1)rep(j,m+1)dp[i][j]=inf;
dp[0][0]=0;
rep(i,n+1){
rep(j,m+1){
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+1);
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+1);
if(a[i]==b[j]){
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]);
}
else{
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+1);
}
}
}
cout<<dp[n][m]<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i,to) for(li i=0;i<((li)(to));i++)
#define repp(i,start,to) for(li i=(li)(start);i<((li)(to));i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v,n) memset(v,(li)n,sizeof(v))
#define bit(n) (1ll<<(li)(n))
#define mp(a,b) make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b) idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c) idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v) for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x,n) idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n) idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x) idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x) idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}
#define read2d(a,w,h) rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h) rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}
typedef pair<li, li> PI;
inline bool check(li x, li m){
while(x>0){
if(x%m==7)return false;
x/=m;
}
return true;
}
int main() {
li n;
cin>>n;
li res=0;
repp(i,1,n+1){
if(check(i,8)&&check(i,10))res++;
}
cout<<res<<endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
bool solve(ll x){
while(x>0){
if(x%10==7) return true;
x/=10;
}
return false;
}
bool solve2(ll x){
while(x>0){
if(x%8==7) return true;
x/=8;
}
return false;
}
int main() {
ll n;
cin >> n;
ll ans=0;
for(ll i=1;i<=n;i++){
if(solve(i)||solve2(i)){
ans++;
}
}
cout<<n-ans<<endl;
return 0;
} |
#include<iostream>
using namespace std;
double a, b;
int main() {
cin >> a >> b;
cout << 100.0l - b * 100 / a;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <numeric>
#include <set>
using namespace std;
using ll = long long;
//ループ
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for(int i = m; i < n; ++i)
//コンテナ
#define all(x) (x).begin(),(x).end()
//関数
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
//-----global variable------------
//-----function definition--------
//-----main-----------------------
int main() {
//----code here------------
double a, b ;
cin >> a >> b ;
cout << std::fixed << std::setprecision(15);
cout << 100.0 * ( a - b ) / a << endl;
//----return---------------
return 0;
}
|
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define li long long int
#define ld long double
#define all(v) v.begin(),v.end()
#define rev(a) reverse(all(a))
#define sort(a) sort(all(a))
#define pb push_back
#define INF 1e18+10
#define MINF -1e18-10
#define rep(i,a,b) for(li i=a;i<b;i++)
#define vli vector<li>
#define MAXN 1e5+10
const li mod = 1e9+7;
using namespace std;
li power(li a, li b) { a%=mod; li ret = 1; while(b){ if(b&1) ret*=a; a*=a;; if(ret>=mod) ret%=mod; if(a>=mod) a%=mod; b>>=1; } return ret; }
template <class T> void read(T& x){
cin>>x;
}
template <class T, class... U> void read(T& x, U&... u){
read(x);
read(u...);
}
template <class A> void read(vector<A>& v){
for(auto &it:v)
read(it);
}
template <class A> void print(A x){
cout<<x;
}
template <class A> void printl(A x){
cout<<x<<endl;
}
template <class A> void print(vector<A>& v)
{
for(auto &it:v){
print(it);
cout<<' ';
}
cout<<endl;
}
void solve()
{
li n;
cin>>n;
vector<pair<li,li> > v;
rep(i,0,n){
li a,b;
cin>>a>>b;
v.pb({a,b});
}
rep(i,0,n){
rep(j,i+1,n){
rep(k,j+1,n){
li x1 = v[i].first;
li y1 = v[i].second;
li x2 = v[j].first;
li y2 = v[j].second;
li x3 = v[k].first;
li y3 = v[k].second;
li area = abs(x1*y2 + x2*y3 + x3*y1 - x1*y3 - x2*y1 - x3*y2);
if(area==0){
cout<<"Yes\n";
return;
}
}
}
}
cout<<"No\n";
}
int main()
{
IOS;
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
// #endif
solve();
return 0;
} | #include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
const long double PI = (acos(-1));
const long long MOD = pow(10, 9) + 7;
int main()
{
int N;
cin >> N;
int *x = new int[N];
int *y = new int[N];
double dt[100][100];
double c[100][100];
for (int i=0; i<N; i++)
{
cin >> x[i] >> y[i];
}
for (int i=0; i<N-1; i++)
{
for (int j=i+1; j<N; j++)
{
int dx = x[i] - x[j];
int dy = y[i] - y[j];
if (dx!=0)
{
dt[i][j] = 1.0*dy/dx;
c[i][j] = y[i]-x[i]*dt[i][j];
}
else
{
dt[i][j] = INFINITY;
c[i][j] = INFINITY;
}
}
}
for (int i=0; i<N-2; i++)
{
for (int j=i+1; j<N-1; j++)
{
for (int k=j+1; k<N; k++)
{
if (dt[i][j] == dt[i][k] && c[i][j] == c[i][k])
{
/*
cout << "i: " << i << " j: " << j << " k: " << k << endl;
cout << "dtij: " << dt[i][j] << " dtik: " << dt[i][k] << endl;
cout << "cij: " << c[i][j] << " cik: " << c[i][k] << endl;
*/
cout << "Yes" << endl;
return 0;
}
}
}
}
cout << "No" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
const double pi = 2.0 * asin(1.0);
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
int main()
{
ll a, b, w;
cin >> a >> b >> w;
w *= 1000;
ll ans1 = 0;
ll ans2 = 0;
rep(i, 1000100)
{
if (a * i <= w && b * i >= w)
{
ans1 = i;
break;
}
}
rep(i, 1000100)
{
if (a * i <= w && b * i >= w)
{
ans2 = i;
}
}
if (ans1 != 0 || ans2 != 0)
{
cout << ans1 << " " << ans2 << endl;
}
else
{
cout << "UNSATISFIABLE" << endl;
}
} | #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <complex>
#include <ctime>
#include <bitset>
// #include <atcoder/all>
#include <fstream>
#include <random>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
using namespace std;
// using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e16;
const ll INF = 1e16;
//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l)); // lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(ll i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause") //デバッグ用
#define ret return 0
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; }
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const long double PI = 3.1415926535897932;
using Graph = vector<vector<ll>>;
bool check(string s, string t)
{
ll sz = 0, so = 0, tz = 0, to = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '0') sz++;
else so++;
if (t[i] == '0') tz++;
else to++;
}
if (sz == tz && so == to)
return true;
else
return false;
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(15); // 15桁表示指定
ll n;
cin >> n;
string s, t;
cin >> s >> t;
if (!check(s, t))
{
cout << -1 << endl;
ret;
}
ll sc = 1;
ll tc = 1;
ll ans = 0;
ll br = false;
for (int i = 0; i < n; i++)
{
br = false;
sc = max(sc, i + 1LL);
tc = max(tc, i + 1LL);
if (t[i] == s[i])
{
continue;
}
if (t[i] != s[i])
{
if (t[i] == '0')
{
for (; sc < n; sc++)
{
if (s[sc] == '0')
{
ans++;
char tmp = s[i];
s[i] = s[sc];
s[sc] = tmp;
sc++;
br = true;
break;
}
}
}
if (br)continue;
if (s[i] == '0')
{
for (; tc < n; tc++)
{
if (t[tc] == '0')
{
ans++;
char tmp = t[i];
t[i] = t[tc];
t[tc] = tmp;
tc++;
br = true;
break;
}
}
}
if (br)continue;
}
}
cout << ans << endl;
return 0;
} |
//Bulbul khan-420...
#include <bits/stdc++.h>
#include <cstring>
#define ll long long int
#define pb() push_back()
#define f(i,n) for(i=0;i<n;i++)
using namespace std;
int main()
{
ll t,a,b,c,d,i,j,k,l,n,m,x,y,z;
cin>>n>>m>>x>>y;
string s[n+6];
for(i=0;i<n;i++)
cin>>s[i];
x--;
y--;
j=0;
if(s[x][y]=='#')
j=0;
else{
for(i=y+1;i<m;i++){
if(s[x][i]=='.')
j++;
else
break;
}
for(i=y-1;i>=0;i--){
if(s[x][i]=='.')
j++;
else
break;
}
for(i=x+1;i<n;i++){
if(s[i][y]=='.')
j++;
else
break;
}
for(i=x-1;i>=0;i--){
if(s[i][y]=='.')
j++;
else
break;
}
j++;
}
cout<<j;
} | #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
long long x, y, a, b;
cin >> x >> y >> a >> b;
long long ans = 0;
while((long double)a * x <= 9e18 and a * x <= b + x and a * x < y){
x *= a;
ans++;
}
long long diff = (y - x - 1)/b;
if(diff > 0ll) ans += diff;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int a[5];
int ans=99999999;
int main()
{
for(int i=1;i<=4;i++)
{
cin>>a[i];
ans=min(ans,a[i]);
}
cout<<ans;
return 0;
} | /***
Bismillahir Rahmanir Rahim
Rabbi Zidni Ilma
Author : Saidur Rahman
Department of CSE, City University, Bangladesh.
***/
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define EPS 1e-6
#define F first
#define sf scanf
#define pf printf
#define S second
#define done return 0
#define MP make_pair
#define pb push_back
#define start int main()
#define PI acos(-1.0)
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define sz(x) int(x.size())
#define all(a) a.begin(),a.end()
#define srt(x) sort(x.begin(),x.end())
#define clock_start clock_t tStart = clock()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define Fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define show_time cout<<"\n\nTime Taken: "<<fixed<<setprecision(3)<<(double)(clock() - tStart) / CLOCKS_PER_SEC<<"s"
/****** Template of some bit operations *****/
int ToggleBit (int n, int X) { return n ^ (1 << X); }
int SetBit (int n, int X) { return n | (1 << X); }
int ClearBit (int n, int X) { return n & ~(1 << X); }
bool CheckBit (int n, int X) { return (bool)(n & (1 << X)); }
///////////////////////////////////////////////////////////////////////////
///////////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////////
//////////////////////////////////////////////////////////////////////////
typedef long long ll;
const int MAXN = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
start
{
Fast;
#ifndef ONLINE_JUDGE
clock_start;
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<min(min(a,b),min(c,d))<<endl;
#ifndef ONLINE_JUDGE
show_time;
#endif
done;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(it, ar) for(auto &it: ar)
#define loop(i, start, end) for(auto i = (start); i <= (end); i++)
#define loopSkip(i, start, end, skip) for(auto i = (start); i <= (end); i += skip)
#define loopRev(i, start, end) for(int i = (start); i >= (end); i--)
#define FLASH ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define testCases(t) cin >> t; while(t--)
#define readLine(s) getline(cin, s);
#define PN(n) cout << (n)
#define PN1(n) cout << (n) << " "
#define PN2(a, b) cout << (a) << " " << (b) << " "
#define PN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define PNN1(n) cout << (n) << "\n"
#define PNN2(a, b) cout << (a) << " " << (b) << "\n"
#define PNN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << "\n"
#define PrintObject(ar) for(auto var: ar) cout << var << " "; cout << "\n"
#define PrintMap(map) for(auto var: map) PNN2(var.first, var.second)
#define PrintPair(pair) PNN2(pair.first, pair.second)
#define IN1(n) cin >> n
#define IN2(a, b) cin >> a >> b
#define IN3(a, b, c) cin >> a >> b >> c
#define ALL(s) s.begin(), s.end()
#define ENTER PNN1("")
#define FLUSH cout.flush()
#define llPair std::pair<lli, lli>
#define iiPair std::pair<int, int>
// #define ONLINE_JUDGE
// Math macros
#define M_PI 3.14159265358979323846
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
template <typename T>
T ii() {
T a;
IN1(a);
return a;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int main(void){
FLASH
#ifndef ONLINE_JUDGE
freopen("input-stream.txt", "r", stdin);
freopen("output-stream.txt", "w", stdout);
#endif
int n;
IN1(n);
vector<int> ar(n);
FOR(it, ar) IN1(it);
sort(ALL(ar));
int total = accumulate(ALL(ar), 0);
int start = 1, end = (int) 1e5;
int ans = INT_MAX;
vector<bool> dp(end + 1, false);
dp[0] = true;
FOR(it, ar) loopRev(i, end, 0) if(dp[i] && i + it <= end) {
dp[i + it] = true;
auto cur = i + it;
if(2 * cur >= total) ans = min(ans, cur);
}
PNN1(ans);
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
const int INF = 1001001001;
int main() {
int n;
cin >> n;
int ans = INF;
rep(i, n) {
int a, p, x;
cin >> a >> p >> x;
if(x > a) {
ans = min(ans, p);
}
}
if(ans == INF) ans = -1;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 1000000007;
string c[4];
LL power(LL a, LL r){
LL res = 1;
for(; r; r >>= 1, a = a * a % mod)
if(r & 1) res = res * a % mod;
return res;
}
LL Fib(int n){
if(n <= 2) return 1;
LL a = 1, b = 1;
for(int i = 0; i < n - 2; i += 1){
LL t = (a + b) % mod;
b = a;
a = t;
}
return a;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
for(int i = 0; i < 4; i += 1) cin >> c[i];
if(c[0] == "A" and c[1] == "A") cout << 1;
else if(c[1] == "B" and c[3] == "B") cout << 1;
else if(c[0] == "A" and c[1] == "B" and c[2] == "A" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
else if(c[0] == "A" and c[1] == "B" and c[2] == "B" and c[3] == "A") cout << Fib(N - 1);
else if(c[0] == "B" and c[1] == "A" and c[2] == "A" and c[3] == "A") cout << Fib(N - 1);
else if(c[0] == "B" and c[1] == "A" and c[2] == "A" and c[3] == "B") cout << Fib(N - 1);
else if(c[0] == "B" and c[1] == "A" and c[2] == "B" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
else if(c[0] == "B" and c[1] == "A" and c[2] == "B" and c[3] == "B") cout << (N == 2 ? 1 : power(2, N - 3));
else if(c[0] == "B" and c[1] == "B" and c[2] == "A" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
else if(c[0] == "B" and c[1] == "B" and c[2] == "B" and c[3] == "A") cout << Fib(N - 1);
else assert(0);
return 0;
}
//AAABABABABABBB | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = 0; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
int main() {
int n;
cin >> n;
int m = 0, day = 1;
do {
m = m + day;
day += 1;
} while(n > m);
cout << day - 1 << endl;
return 0;
}
|
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld",n)
#define pri(n) printf("%d",n)
#define pln() printf("\n")
#define priln(n) printf("%d\n",n)
#define lpriln(n) printf("%lld\n",n)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
#define inf INT_MAX
#define linf LLONG_MAX
#define lninf LLONG_MIN
const int mod = 1e9 + 7;
const int N = 1e5 + 4;
typedef long double ld;
int solve()
{
int n, m, k; scn(n); scn(m); scn(k);
pair<ld, ld> p = {0.0, 0.0};
vector<int> mark(n + 1, 0);
rep(i, 0, k)
{
int val; scn(val);
mark[val]++;
}
int cnt = 0;
rep(i, 1, n)
{
if(mark[i])
cnt++;
else
{
if(cnt >= m)
{
pri(-1); return 0;
}
cnt = 0;
}
}
if(cnt >= m)
{
pri(-1); return 0;
}
vector<pair<ld, ld>> v(n + m + 1, {0.0, 0.0});
for(int i = n - 1; i >= 0; i--)
{
p.F -= v[i + m + 1].F; p.S -= v[i + m + 1].S;
if(mark[i])
{
v[i].F = 1.0;
}
else
{
v[i].F = p.F / (ld)m; v[i].S = p.S / (ld)m;
v[i].S += 1.0;
}
// cout << v[i].F << " " << v[i].S << endl;
if(i == 0)
cout << fixed << setprecision(10) << v[i].S / (1.0 - v[i].F);
p.F += v[i].F; p.S += v[i].S;
}
return 0;
}
int main()
{
int t = 1; //scn(t);
while(t--)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#define int long long
using ld = long double;
const int N = 2e5 + 7;
const ld eps = 1e-9;
ld a[N], b[N], csum_a[N], csum_b[N];
int mrkd[N];
int32_t main() {
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m, k; cin >> n >> m >> k;
vector <int> o(k);
for(int &x: o) cin >> x, mrkd[x] = true;
ld q = 1.0 / (ld)m;
for(int i = n - 1; i >= 0; i--) {
for(int x: o) if(i + m >= x && x > i) {
b[i] += q;
}
a[i] = 1.0;
a[i] += q * (csum_a[i + 1] - csum_a[i + m + 1]);
b[i] += q * (csum_b[i + 1] - csum_b[i + m + 1]);
if(mrkd[i]) {
a[i] = b[i] = 0.0;
}
csum_a[i] = csum_a[i + 1] + a[i];
csum_b[i] = csum_b[i + 1] + b[i];
}
if(fabs(1.0 - b[0]) < eps) {
cout << -1 << endl;
return 0;
}
cout << fixed << setprecision(10) << a[0] / (1.0 - b[0]) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = long long;
const ll MOD = 1e9+7;
ll pow_mod(ll a, ll n, ll p)
{
ll res = 1;
while(n > 0){
if(n & 1) res = res*a%p;
a = a*a%p;
n >>= 1;
}
return res;
}
int main()
{
ll i,j;
ll h,w;
cin >> h >> w;
vector<string> s(h);
rep(i,h) cin >> s[i];
vector<vector<int>> yoko(h);
vector<vector<int>> tate(w);
int k = 0;
rep(i,h) rep(j,w) if(s.at(i).at(j) == '.') ++k;
rep(i,h){
yoko.at(i).push_back(-1);
rep(j,w){
if(s.at(i).at(j) == '#') yoko.at(i).push_back(j);
}
yoko.at(i).push_back(w);
}
rep(i,w){
tate.at(i).push_back(-1);
rep(j,h){
if(s.at(j).at(i) == '#') tate.at(i).push_back(j);
}
tate.at(i).push_back(h);
}
ll ans = 0;
rep(i,h){
rep(j,w){
if(s.at(i).at(j) == '#') continue;
ll tmp = 0;
ll ind1 = lower_bound(all(yoko.at(i)), j) - yoko.at(i).begin();
tmp += yoko.at(i).at(ind1) - yoko.at(i).at(ind1 - 1) - 1;
ll ind2 = lower_bound(all(tate.at(j)), i) - tate.at(j).begin();
tmp += tate.at(j).at(ind2) - tate.at(j).at(ind2 - 1) - 1;
--tmp;
ans += (pow_mod(2, tmp, MOD) + MOD - 1)%MOD*(pow_mod(2, k - tmp, MOD))%MOD;
ans %= MOD;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
typedef long long int64;
int main() {
#ifdef LOCAL
freopen(".a.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> S(n);
vector<vector<int>> A(n, vector<int>(m)), B(n, vector<int>(m)), C(n, vector<int>(m)), D(n, vector<int>(m));
int K = 0;
for (int i = 0; i < n; ++i) cin >> S[i], K += count(S[i].begin(), S[i].end(), '.');
int64 md = 1e9 + 7;
vector<int64> two(max(100, K + 2));
two[0] = 1;
for (int i = 1; i < SZ(two); ++i) two[i] = two[i - 1] * 2 % md;
int64 res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (S[i][j] == '.') {
A[i][j] = 1;
if (i - 1 >= 0) A[i][j] += A[i - 1][j];
B[i][j] = 1;
if (j - 1 >= 0) B[i][j] += B[i][j - 1];
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
if (S[i][j] == '.') {
C[i][j] = D[i][j] = 1;
if (i + 1 <= n - 1) C[i][j] += C[i + 1][j];
if (j + 1 <= m - 1) D[i][j] += D[i][j + 1];
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
if (S[i][j] == '.') {
int tot = A[i][j] + B[i][j] + C[i][j] + D[i][j] - 3;
(res += (two[tot] + md - 1) * two[K - tot] % md) %= md;
}
}
cout << res;
return 0;
}
//14.10.2020 04:31:12 CST
|
#include <iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int maxn = 5005;
int main()
{
char s[5005];
int a[maxn], g[maxn], c[maxn], t[maxn];
int n;
int cnt = 0;
cin >> n;
a[0] = 0;
g[0] = 0;
c[0] = 0;
t[0] = 0;
int a1, g1, c1, t1;
for(int i = 1; i <= n; i++)
{
cin >> s[i];
}
for(int i = 1; i <= n; i++)
{
if(s[i] == 'A')
{
a[i] = a[i - 1] + 1;
}
else a[i] = a[i - 1];
if(s[i] == 'G')
{
g[i] = g[i - 1] + 1;
}
else g[i] = g[i - 1];
if(s[i] == 'C')
{
c[i] = c[i - 1] + 1;
}
else c[i] = c[i - 1];
if(s[i] == 'T')
{
t[i] = t[i - 1] + 1;
}
else t[i] = t[i - 1];
}
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
if((a[j] - a[i - 1] == t[j] - t[i - 1]) && (c[j] - c[i - 1] == g[j] - g[i - 1]))
cnt++;
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main() {
int N;
string S;
cin >> N >> S;
int ans = 0;
rep(i, N) {
int c1 = 0, c2 = 0;
for (int j = i; j < N; j++) {
if (S[j] == 'A')
c1++;
else if (S[j] == 'T')
c1--;
else if (S[j] == 'G')
c2++;
else if (S[j] == 'C')
c2--;
if (c1 == 0 && c2 == 0) ans++;
}
}
cout << ans << endl;
return 0;
} |
// Problem: D - Base n
// Contest: AtCoder - AtCoder Beginner Contest 192
// URL: https://atcoder.jp/contests/abc192/tasks/abc192_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
// Disclaimer: Don't copy my template, it may lead to plagiarism.
/* Author: Soumy Jain
Handle: soumy_jain || soumyjain
"Beautiful flowers too, eventually wither and fall. That's the fate of all living beings."
"I hate perfection. To be perfect is to be unable to improve any furthur." - Mayuri Kurotsuchi | Bleach
"I smile to show the pressure of heroes and to trick the fear inside of me."
"Gravel may be gravel, but me? I'm the gravel that shatters diamonds."
"If you were to write a story with me in the lead role, it would certainly be...a TRAGEDY." - Kaneki Ken | Tokyo
Ghoul
*/
/******************************************************************************/
// clang-format off
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define SPEEDHACK ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Tc ll tc; cin>>tc; while(tc--)
#define Qu ll qu; cin>>qu; while(qu--)
#define ff first
#define ss second
#define sz(v) (ll)(v).size()
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MOD 1000000007 // 998244353
using namespace std;
/******************************************************************************/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(ll x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(ull 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
// clang-format on
/***********************************MAIN***************************************/
// Are you ready to face the wrath of test cases? Good luck noob Soumy!
int main()
{
SPEEDHACK
// file
string s;
cin >> s;
ll m;
cin >> m;
ll d = 0;
for (ll i = 0; i < sz(s); i++)
{
d = max(d, (ll)s[i] - 48);
}
if (sz(s) == 1)
{
if (s[0] - 48 > m)
cout << 0;
else
cout << 1;
return 0;
}
ll c = 0;
if (sz(s) == 2 || sz(s) == 3 || sz(s) == 4)
{
for (ll i = pow(m / (s.front() * 1.0 - 48), 1.0 / (sz(s) - 1)); i > d; i--)
{
ll k = 0, b = 1;
for (ll j = sz(s) - 1; j >= 0; j--, b *= i)
{
k += (s[j] - 48) * b;
}
if (k <= m)
{
c = i;
break;
}
}
cout << c - d;
return 0;
}
for (ll i = d + 1; i <= pow(m, 1.0 / (sz(s) - 1)); i++)
{
ll k = 0, b = 1;
for (ll j = sz(s) - 1; j >= 0; j--, b *= i)
{
k += (s[j] - 48) * b;
}
if (k > m)
break;
c++;
}
cout << c;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
void solve(std::string X, long long M){
if(X.size()==1){
ll x=stoll(X);
if(x<=M){
cout<<1<<endl;
return;
}else{
cout<<0<<endl;
return;
}
}
ll maxchar=0;
for (auto &&i : X)
{
ll ii=i-'0';
chmax(maxchar,ii);
}
ll r=M+1,l=maxchar;
while(l+1<r){
ll mid=(l+r)/2;
ll tmp=0;
bool f=false;
for (auto &&i : X)
{
ll ii=i-'0';
if(tmp>(M-ii)/mid){
f=true;
break;
}
tmp=tmp*mid+ii;
if(tmp>M){
f=true;
break;
}
}
if(f){
r=mid;
}else{
l=mid;
}
}
cout<<l-maxchar<<endl;
}
int main(){
cout<<setprecision(1000);
std::string X;
std::cin >> X;
long long M;
scanf("%lld",&M);
solve(X, M);
return 0;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update>
#define int long long int
#define ull unsigned long long
#define pb push_back
#define inf 1e18
#define mk make_pair
#define ld long double
#define mod 1000000007
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test int t; cin>>t; while(t--)
#define setbits __builtin_popcount
#define endl '\n'
#define LOCAL
#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__) << "] "
int32_t main()
{
fastIO;
int a,b,c,d;
cin>>a>>b>>c>>d;
int x,y,val,val1;
val=(b+c-1)/c;
if(val>d)
{
cout<<-1<<endl;
}
else{
x=a;
y=(d*c);
y-=b;
if(y==0)
{
cout<<-1<<endl;
}
else{
val=(x+y-1)/y;
cout<<val<<endl;
}
}
} | #include<bits/stdc++.h>
using namespace std;
#define fast {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
typedef long long int ll;
typedef string S;
#define M 1e18
#define AS 250005
#define sp cout<<' '
#define nw cout<<endl
#define rt return
#define __ template<typename T, typename... Types>
void in() {rt;} __ void in(T &a, Types&... b){cin>>(a); in(b...);}
void o() {rt;} __ void o(T a, Types... b){cout<<(a);sp; o(b...);}
#define fr(Hi,a,n) for(ll Hi=a;Hi<=n;Hi++)
#define frm(Hi,a,n) for(ll Hi=n;Hi>=a;Hi--)
#define P pair<ll,ll>
#define vc vector<ll>
#define pb push_back
#define MP map<ll,ll>
bool sortin(const pair<ll,ll> &e,const pair<ll,ll> &f){return (e.first<f.first);}
bool POT(ll x){return x && (!(x&(x-1)));}
ll i,j,k,l,m,n,p,q,r,a,b,c,x,y,z,ts,mn=1e18,mod=1e9+7;
ll ar[AS],br[AS],xr[AS],tem[AS];
int main()
{
fast;
in(a,b,c,x);
if(c*x<=b)o(-1);
else
{
p=c*x-b;
o((a+p-1)/p);
}
}
|
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
#pragma endregion library
int main() {
int n, m;
cin >> n >> m;
vi w(n);
rep(i, n) cin >> w[i];
vector<pll> vl;
rep(i, m) {
int v, l;
cin >> l >> v;
vl.emplace_back(v, l);
}
sort(all(vl), [](const pll &a, const pll &b) {
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
});
if (vl[0].first < *max_element(all(w))) {
cout << -1 << '\n';
return 0;
}
ll now = 0;
rep(i, m) {
chmax(now, vl[i].second);
vl[i].second = now;
}
vi perm(n);
iota(all(perm), 0);
ll ans = LINF;
do {
vll wnow(n);
rep(i, n) {
wnow[i] = w[perm[i]];
}
vll dist(n);
rep(r, n) rep(l, r) {
if (r - l <= 0) continue;
ll now = 0;
repi(i, l, r + 1) {
now += wnow[i];
}
auto it = lower_bound(all(vl), pll{now, -1});
int idx = it - vl.begin() - 1;
ll limit = 0;
if (idx >= 0) {
limit = vl[idx].second;
}
repi(i, l + 1, r) {
limit -= dist[i];
}
chmax(dist[r], limit);
}
ll res = 0;
rep(i, n) {
res += dist[i];
}
chmin(ans, res);
} while (next_permutation(all(perm)));
cout << ans << '\n';
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<n+1;++i)
int main(){
int N,K,sum=0;
cin >> N >> K;
rep(i,N){
rep(j,K){
sum+=100*i +j;
}
}
cout << sum <<endl;
return 0;
} |
#include<bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pi pair<ll,ll>
#define endl "\n"
using namespace std;
const ll N=100010;
vector<pi> adj[N];
vector<vector<ll> > dis;
void func(ll start)
{
set<pi> s;
s.insert(mp(0,start));
int firstdone=0;
while(!s.empty()){
ll v=(*s.begin()).second;
ll d=(*s.begin()).first;
s.erase(s.begin());
if(dis[start][v]<=d){continue;}
if(v==start){
if(firstdone==0){
firstdone=1;
}
else{
dis[start][v]=d;
}
}
else{
dis[start][v]=d;
}
for(auto x:adj[v]){
ll u=x.first,w=x.second;
if(dis[start][u]>d+w){
s.insert(mp(d+w,u));
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m;
cin>>n>>m;
for(ll i=0;i<m;++i){
ll a,b,w;
cin>>a>>b>>w;
adj[a].pb(mp(b,w));
}
dis.resize(n+1,vector<ll>(n+1,INT_MAX));
for(ll i=1;i<=n;++i){
func(i);
}
for(ll i=1;i<=n;++i){
if(dis[i][i]==INT_MAX){
dis[i][i]=-1;
}
cout<<dis[i][i]<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
struct edge{
int to;
ll cost;
edge(){}
edge(int a, ll b) : to(a), cost(b){}
};
using ve = vector<edge>;
void dijkstra(int s, vec &dist, vector<ve> &G){
priority_queue<P, vector<P>, greater<> > pque;
for(edge &e : G[s]){
if(chmin(dist[e.to], e.cost)){
pque.emplace(dist[e.to], e.to);
}
}
while(!pque.empty()){
P p = pque.top(); pque.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(const edge &e : G[v]){
if(chmin(dist[e.to], dist[v] + e.cost)){
pque.emplace(dist[e.to], e.to);
}
}
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin>>N>>M;
vector<ve> G(N);
rep(i, M){
int a, b;
ll c;
cin>>a>>b>>c;
--a; --b;
G[a].emplace_back(b, c);
}
rep(i, N){
vec dist(N, INF);
dijkstra(i, dist, G);
cout<<(dist[i] == INF ? -1 : dist[i])<<endl;
}
} |
// Never gonna give you up
// Never gonna let you down
// Never gonna run around and desert you
#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
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD=1e9+7;
const int INF32=1<<30;
const ll INF64=1LL<<60;
const ld pi=acos(-1);
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);}
ll mul(ll a, ll b) {return a*b%MOD;}
ll modpow(ll b, ll i){
ll s=1;
while(i){
if(i%2) s=(s*b)%MOD;
b=(b*b)%MOD; i/=2;
}
return s;
}
ll inv(ll a) {return modpow(a,MOD-2);}
void solve(){
int h,w,n,m; cin >>h >>w >>n >>m;
int a[h][w],cnt=0; memset(a,-1,sizeof(a));
while(n--){int x,y; cin >>x >>y; a[x-1][y-1]=1;}
while(m--){int x,y; cin >>x >>y; a[x-1][y-1]=0;}
for(int i=0; i<h; i++) for(int j=0; j<w; j++) if(a[i][j]==1){
for(int k=i-1; k>=0; k--){if(a[k][j]==-1||a[k][j]==2) a[k][j]=2; else break;}
for(int k=i+1; k<h; k++){if(a[k][j]==-1||a[k][j]==2) a[k][j]=2; else break;}
for(int k=j-1; k>=0; k--){if(a[i][k]==-1||a[i][k]==2) a[i][k]=2; else break;}
for(int k=j+1; k<w; k++){if(a[i][k]==-1||a[i][k]==2) a[i][k]=2; else break;}
}
for(int i=0; i<h; i++) for(int j=0; j<w; j++) if(a[i][j]>0) cnt++;
cout <<cnt;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll M = 1e18;
int si[] = {-1, 0, 1, 0};
int sj[] = {0, -1, 0, 1};
int main(){
int h, w, n, m;
cin >> h >> w >> n >> m;
vector<vector<int>> v(h, vector<int>(w,0));
rep(i,n){
int a,b;
cin >> a >> b;
a--; b--;
v[a][b] = 1;
}
rep(i,m){
int c,d;
cin >> c >> d;
c--; d--;
v[c][d] = -1;
}
vector<vector<int>> x(h,vector<int>(w,0));
for(int i = 0; i < h; i++){
bool flag = false;
for(int j = 0; j < w; j++){
if(v[i][j] == 1) flag = true;
else if(v[i][j] == -1) flag = false;
if(flag) x[i][j]++;
}
}
for(int i = 0; i < h; i++){
bool flag = false;
for(int j = w-1; j >= 0; j--){
if(v[i][j] == 1) flag = true;
else if(v[i][j] == -1) flag = false;
if(flag) x[i][j]++;
}
}
for(int j = 0; j < w; j++){
bool flag = false;
for(int i = 0; i < h; i++){
if(v[i][j] == 1) flag = true;
else if(v[i][j] == -1) flag = false;
if(flag) x[i][j]++;
}
}
for(int j = 0; j < w; j++){
bool flag = false;
for(int i = h-1; i >= 0; i--){
if(v[i][j] == 1) flag = true;
else if(v[i][j] == -1) flag = false;
if(flag) x[i][j]++;
}
}
int ans = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(x[i][j] != 0) ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> P;
typedef tuple<int,int,int> tii;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<pll,pll> ppll;
#define rep(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define bit(n,k) (n>>k&1)
ll INFL = ll(5e18)+5;
int INF = 1001001001;
const ll M = (ll)(1e9)+7;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,1,-1};
int main(){
string x; cin >> x;
int n = x.size();
string ans = "";
rep(i,0,n){
if(x[i]=='.') break;
ans += x[i];
}
cout << ans << endl;
}
| // raggarwalg01
#include<bits/stdc++.h>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);cin.tie(NULL);typedef int MyCustomType;
string s{};
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]=='.')
break;
else cout<<s[i];
}
} |
#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; }
int main(){
ll a,b,c,d; cin >> a >> b >> c >> d;
if(c*d-b <= 0){
cout << "-1" << endl;
return 0;
}
else{
cout << (a+c*d-b-1)/(c*d-b) << endl;
}
}
| #include <cstdio>
#include <cctype>
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
inline int read (void) {
int x = 0, f = 1, ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
const int maxn = 20;
int h, w, ans, tot1, tot2;
bool flag[maxn][maxn];
void dfs (int x, int y) {
if(y > w) { y = 1; ++ x; }
if(x > h) { ++ ans; return ; }
if(flag[x][y]) dfs (x, y+1);
else {
if(tot2) {
-- tot2; flag[x][y] = 1;
dfs (x, y+1);
++ tot2; flag[x][y] = 0;
}
if(tot1 && y + 1 <= w && !flag[x][y+1]) {
-- tot1; flag[x][y] = flag[x][y+1] = 1;
dfs (x, y+2);
++ tot1; flag[x][y] = flag[x][y+1] = 0;
}
if(tot1 && x + 1 <= h && !flag[x+1][y]) {
-- tot1; flag[x][y] = flag[x+1][y] = 1;
dfs (x, y+1);
++ tot1; flag[x][y] = flag[x+1][y] = 0;
}
}
}
int main (void) {
h = read(), w = read(), tot1 = read(), tot2 = read();
dfs (1, 1);
printf("%d\n", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t X;
cin >> N >> X;
X *= 100;
int64_t ans = 0;
for (int i = 0; i < N; i++) {
int64_t V,P;
cin >> V >> P;
ans += V*P;
if(ans > X){
cout << i+1 << endl;
return 0;
}
}
cout << -1 << endl;
} | #include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
ll a[200005];
int main(){
ll n,x;
cin>>n>>x;
ll tot=0;
ll sol=-1;
for(ll i=1;i<=n;i++){
ll v,p;
cin>>v>>p;
tot+=v*p;
if(tot>x*100 and sol==-1){
sol=i;
}
}
cout<<sol;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=2e+5+10;
pair<long double,long double> dp[N];
int A[11],n,m,k;
bool isp[N];
bool ispos()
{
int cnt=0;
for(int i=1;i<=n;i++)
{
if(isp[i]==1)
cnt++;
else
cnt=0;
if(cnt==m)
return false;
}
return true;
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=k;i++)
{
cin>>A[i];
isp[A[i]] = true;
}
if(!ispos())
{
cout<<-1;
return 0;
}
pair<long double,long double> S={0,0};
for(int i=n-1;i>=0;i--)
{
S.first -= dp[i+m+1].first;
S.second -= dp[i+m+1].second;
if(isp[i])
{
dp[i]={1,0};
}
else
{
dp[i]={S.first/(long double)m,1+S.second/(long double)m};
}
S.first += dp[i].first;
S.second += dp[i].second;
}
long double ans = dp[0].second/(1 - dp[0].first);
cout<<setprecision(10)<<fixed<<ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define PER(i, a, b) for(int i = (a); i >= (b); i--)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAX = 1e5+5;
int N, M, K, x, a[MAX];
lf sum[2*MAX];
lf solve(lf m) {
PER(i, N-1, 0) {
if(a[i]) sum[i] = m + sum[i+1];
else sum[i] = 1 + (lf)((0.0 + sum[i+1] - sum[i+M+1]) / M) + sum[i+1];
}
return sum[0] - sum[1];
}
lf search(lf i, lf j, int k) {
lf m = (i+j) / 2.0;
if(!k) return m;
if(solve(m) >= m) return search(m, j, k-1);
else return search(i, m, k-1);
}
int main(int argc, char ** argv) {
cout << fixed << setprecision(6);
scanf("%d%d%d", &N, &M, &K);
rep(i, 0, K) {
scanf("%d", &x);
a[x] = true;
}
lf ans = search(1, 1e12, 100);
if(abs(ans-1e12) < 1e-5) puts("-1");
else cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const int N = 2e5 + 20;
typedef long long LL;
typedef pair<LL, LL> PLL;
int n, m, q;
struct Node
{
int l, r;
LL sum, cnt;
}tra[N << 2], trb[N << 2];
int a[N], b[N];
int t[N], x[N], y[N];
vector<int> nums;
void push_up(Node tr[], int u)
{
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
tr[u].cnt = tr[u << 1].cnt + tr[u << 1 | 1].cnt;
}
void build(Node tr[], int u, int l, int r)
{
tr[u] = {l, r, 0, 0};
if(l == r) return;
int mid = l + r >> 1;
build(tr, u << 1, l, mid);
build(tr, u << 1 | 1, mid + 1, r);
}
void modify(Node tr[], int u, int x, int d)
{
if(tr[u].l == x && tr[u].r == x)
{
tr[u].cnt += d;
tr[u].sum += nums[x] * d;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if(x <= mid) modify(tr, u << 1, x, d);
else modify(tr, u << 1 | 1, x, d);
push_up(tr, u);
}
PLL query(Node tr[], int u, int l, int r)
{
if(tr[u].l >= l && tr[u].r <= r)
{
return {tr[u].cnt, tr[u].sum};
}
int mid = tr[u].l + tr[u].r >> 1;
PLL res = {0, 0}, right = {0, 0}, left = {0, 0};
if(l <= mid) left = query(tr, u << 1, l, r);
if(r > mid) right = query(tr, u << 1 | 1, l, r);
res.first = left.first + right.first;
res.second = left.second + right.second;
return res;
}
int find(int x)
{
return lower_bound(nums.begin(), nums.end(), x) - nums.begin();
}
int main()
{
scanf("%d%d%d", &n, &m, &q);
for(int i = 1; i <= q; ++ i)
{
scanf("%d%d%d", &t[i], &x[i], &y[i]);
nums.push_back(y[i]);
}
nums.push_back(0);
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
LL res = 0;
build(tra, 1, 0, nums.size() - 1);
build(trb, 1, 0, nums.size() - 1);
for(int i = 1; i <= q; ++ i)
{
if(t[i] == 1)
{
auto p = query(trb, 1, find(a[x[i]]) + 1, nums.size() - 1);
modify(tra, 1, find(a[x[i]]), -1);
res -= p.second + (m - p.first) * a[x[i]];
a[x[i]] = y[i];
modify(tra, 1, find(a[x[i]]), 1);
p = query(trb, 1, find(a[x[i]]) + 1, nums.size() - 1);
res += p.second + (m - p.first) * a[x[i]];
}
else
{
auto p = query(tra, 1, find(b[x[i]]) + 1, nums.size() - 1);
modify(trb, 1, find(b[x[i]]), -1);
res -= p.second + (n - p.first) * b[x[i]];
b[x[i]] = y[i];
modify(trb, 1, find(b[x[i]]), 1);
p = query(tra, 1, find(b[x[i]]) + 1, nums.size() - 1);
res += p.second + (n - p.first) * b[x[i]];
}
printf("%lld\n", res);
}
return 0;
} | #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 5;
#define T pair<pair<ll, ll>, bool>
int main()
{
ll n, c;
scanf("%lld%lld", &n, &c);
priority_queue<T, vector<T>, greater<T>> pq;
for (ll i = 0; i < n; i++)
{
ll a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
pq.push({ {a, c}, 1 });
pq.push({ {b + 1, c}, 0 });
}
ll sum = 0;
ll cur = 0;
while (!pq.empty())
{
ll day = pq.top().first.first;
while (!pq.empty() && pq.top().first.first == day)
{
ll num = pq.top().first.second;
bool type = pq.top().second;
if (type) cur += num;
else cur -= num;
pq.pop();
}
if (pq.empty()) break;
ll nxt = pq.top().first.first;
sum += min(cur, c) * (nxt - day);
}
printf("%lld", sum);
return 0;
} |
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORX(i, a, b, x) for (ll i = (a); i <= (b); i+=x)
#define debug(x) cout <<" || "<< #x << " is= " << x<<endl; //use for debug
#define read(a) ll a; cin >> a;
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define pr pair<ll,ll>
#define vpll vector<pair<ll,ll>>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend() //reverse of above;
#define sz(container) int((container).size())
#define cut(x) {cout<<x<<"\n"; continue;}
#define setprec(x) cout << fixed << showpoint << setprecision(15)<<x<<"\n";
#define time cout << clock() / double(CLOCKS_PER_SEC) << endl;
#define endl '\n';
#define mod2 1000000007
#define mod 998244353
#define pi 3.141592653589793238
#define fi first
#define se second
#define test(i) cout<<"Case #"<<i<<": ";
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//*s.find_by_order() ->returns an iterator to the k-th largest element (counting from zero);
//s.order_of_key() ->number of elements strictly smaller then our item;
//st.rbegin()->second; last element in set
//st.erase(prev(st.end())); delete last element in set;
//sort(a+1,a+n+1,[](ll x,ll y){return abs(x)>abs(y);});
//sort(v.begin(), v.end(), greater<int>()); -> not sure work for first also if second are equal;
//q=lower_bound(all(v[a[i]]),i)-v[a[i]].begin()+1;
// s.substr(start_ind,length);
// vpll shift={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}} ->shift operator;
//bool comp(pr a,pr b){return a.second<b.second;} // use for differentiate order of comparision;
// cntbit(x) __builtin_popcount(x); -> no. of bits in given no.;
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
const ll MAXN=2e7+5;
const ll inf=1e18+5;
int main()
{
fast_io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//“Don’t wish it were easier. Wish you were better.” – Jim Rohn;
read(t);
while(t--)
{
ll n; cin>>n;
ll cnt=0;
while(n%2==0)
{
cnt++;
n/=2;
}
if(cnt==0) cut("Odd");
if(cnt==1) cut("Same");
cut("Even");
}
} | #include <bits/stdc++.h>
#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//declare here -------------------------------------------------------------------------------
const int N =1e5+2;
// solve here ---------------------------------------------------------------------------------
// read the problem carefull
// have rough idea of code before writing
void solve(){
ll l ,r ;
cin>>l>>r ;
ll val = r- 2*l +1 ;
if(val<0){ cout<<0; return ;}
val= (val*(val+1))/2 ;
cout<<val ;
}
//--------------------------------------------------------------------------------------------
// make sure boundries are correct and all imputs are taken properly
//check for edge cases and boundary values
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
long long test =1 ;
cin>>test ;
while(test--){
solve() ;
cout<<"\n" ;
}
return 0 ;
} |
#include <bits/stdc++.h>
using namespace std;
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define MAX(v) (*max_element((v).begin(), (v).end()))
#define MIN(v) (*min_element((v).begin(), (v).end()))
#define pb push_back
#define FOR(i, n) for(int i = 0; i < (n); i++)
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
ll exp(ll base, ll e, ll md) {
ll mul = base % md;
ll res = 1;
while(e){
if(e & 1) res = (res*mul) % md;
e >>= 1;
mul = mul*mul % md;
}
return res;
}
const int N = 2e5 + 5;
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int n, m; cin>>n>>m;
int cnt[2] = {};
ll ans = 0;
FOR(i, n){
bool par = 0;
FOR(i, m){
char c; cin>>c;
par ^= c == '1';
}
ans += cnt[!par];
cnt[par]++;
}
cout<<ans<<"\n";
} | #include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,m;
string str[MAXN];
int main()
{
scanf("%d%d",&n,&m);
int cnt=0;
for(int i=0;i<n;i++)
{
cin>>str[i];
int s=0;
for(int j=0;j<m;j++) if(str[i][j]=='1') s++;
if(s&1) cnt++;
}
printf("%lld\n",1LL*cnt*(n-cnt));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
using ll = long long;
#define endl "\n";
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x);
#define pvector(x) for(auto i : x)cout << i << " ";cout << "\n";
#define ivector(x) for(auto &i : x)cin >> i;
const int MOD = 1e9 + 7;
int main() {
fast;
auto S = [](int n){
string s = to_string(n);
int sum=0;
for(char i : s)sum += (i-'0');
return sum;
};
int a,b; cin >> a >> b;
cout << max(S(a),S(b)) << "\n";
}
| /*
solution and tut used...
credits : @yash_daga
author : Aryan Agarwal, IIT KGP
created : 13-December-2020 19:44:17 IST
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxn = 1e5;
const long long INF = 2e18;
const int32_t M = 1000000007; /*more than (10)^9*/ /*7 + 1e9*/
// const int32_t M = 998244353; /*less than (10)^9*/ /*1 + 7*17*(2)^23*/
const long double pie = acos(-1);
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)
#define dbg(a) cout<<"["<<#a<<" : "<<a<<"]\n"
#define dbg2(a,b) cout<<"["<<#a<<" : "<<a<<"], "<<"["<<#b<<" : "<<b<<"]\n"
#define dbg3(a,b,c) cout<<"["<<#a<<" : "<<a<<"], "<<"["<<#b<<" : "<<b<<"], "<<"["<<#c<<" : "<<c<<"]\n"
void solve()
{
int n,m;
cin>>n>>m;
int a[n+1],b[m+1];
F(i,1,n)cin>>a[i];
F(i,1,m)cin>>b[i];
int dp[n+1][m+1];
dp[0][0]=0;
F(i,1,n)
{
F(j,1,m)
{
dp[i][j]=n+m;
}
}
F(i,1,n)dp[i][0]=i;
F(i,1,m)dp[0][i]=i;
F(i,1,n)
{
F(j,1,m)
{
dp[i][j]=min(dp[i-1][j]+1,dp[i][j]);
dp[i][j]=min(dp[i][j-1]+1,dp[i][j]);
dp[i][j]=min(dp[i-1][j-1]+(a[i]!=b[j]),dp[i][j]);
}
}
cout<<dp[n][m];
}
signed 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
#ifdef ARYAN_SIEVE
// defualt mxn_sieve = 1e5
sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
// default [L,R] = [1,1e5]
segmented_sieve();
#endif
#ifdef ARYAN_FACT
// default mxn_fact = 1e5
fact_init();
#endif
// cout<<fixed<<setprecision(10);
int _t=1;
// cin>>_t;
for (int i=1;i<=_t;i++)
{
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} |
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, n) for(ll i = 1; i < n + 1; i++)
#define PI 3.14159265359
#define EPS 0.0000000001
#define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;
bool checkSubstring(string s){
ll firstZeroP = -1;
rep(i, s.size()){
if(firstZeroP == -1){
if(i == 0 && s[i] == '0'){
firstZeroP = 0;
}else if(i == 1 && s[i] == '0'){
firstZeroP = 1;
}else if(i == 2 && s[i] == '0'){
firstZeroP = 2;
}else if(i == 2 && s[i] == '1'){
return false;
}
continue;
}
if(s[i] == '0'){
if((i - firstZeroP) % 3 != 0){
return false;
}
}else{
if((i - firstZeroP) % 3 == 0){
return false;
}
}
}
return true;
}
int main(){
ll N;
string T;
cin >> N;
cin >> T;
if(T == "1"){
cout << 20000000000 << endl;
return 0;
}
if(!checkSubstring(T)){
cout << 0 << endl;
}else{
ll countZero = 0;
rep(i, T.size()){
if(T[i] == '0'){
countZero++;
}
}
if(T[T.size() - 1] == '0'){
cout << 10000000000 - countZero + 1 << endl;
}else{
cout << 10000000000 - countZero << endl;
}
}
return 0;
}
| #include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> po, ne;
int a, b;
int main(void) {
scanf("%d %d", &a, &b);
for (int i = 1; i <= a; i++) {
po.push_back(i);
}
for (int i = 1; i <= b; i++) {
ne.push_back(-i);
}
if (po.size() < ne.size()) {
for (int i = po.size(); i < ne.size(); i++) {
po[po.size() - 1] -= ne[i];
}
}
else if (ne.size() < po.size()) {
for (int i = ne.size(); i < po.size(); i++) {
ne[ne.size() - 1] -= po[i];
}
}
for (int e : ne) {
printf("%d ", e);
}
for (int e : po) {
printf("%d ", e);
}
puts("");
} |
#include <algorithm>
#include <bits/stdc++.h>
#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 chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define even(x) (x % 2 == 0 ? true : false)
#define odd(x) (x % 2 ? true : false)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;
//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;
const int mod = 1e9 + 7;
//vector<vector<int>> dp(m, vector<int>(n));
struct mint {
uint x;
mint(): x(0) {}
mint(ll x):x((x%mod+mod)%mod) {}
mint operator-() const { return mint(0) - *this;}
mint operator~() const { return mint(1) / *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=(ull)x*a.x%mod; return *this;}
mint& operator/=(const mint& a) { x=(ull)x*a.pow(mod-2).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 operator/(const mint& a) const { return mint(*this) /= a;}
mint pow(ll t) const {
if(!t) return 1;
mint res = pow(t/2);
res *= res;
return (t&1)?res*x:res;
}
mint inv() const { return pow(mod-2);}
bool operator<(const mint& a) const { return x < a.x;}
bool operator>(const mint& a) const { return x > a.x;}
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;}
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(all(a));
mint ans = a[0] + 1;
for (int i = 1; i < n; i++) {
ans *= a[i] - a[i - 1] + 1;
}
cout << ans << endl;
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
ll dis(vector<ll> r, vector<ll> g) {
ll mi = 1000100100100100100;
ll gn = g.size();
ll rn = r.size();
if (gn == 0 || rn == 0)
return mi;
for (auto a : r) {
ll i = lower_bound(g.begin(), g.end(), a) - g.begin();
if (gn > i) {
mi = min(mi, abs(a - g[i]));
}
if (i > 0)
mi = min(mi, abs(a - g[i - 1]));
}
return mi;
};
int main() {
ll N;
cin >> N;
vector<ll> rs;
vector<ll> gs;
vector<ll> bs;
rep(i, 0, 2 * N) {
ll cute;
string color;
cin >> cute >> color;
if (color == "R") {
rs.push_back(cute);
}
if (color == "G") {
gs.push_back(cute);
}
if (color == "B") {
bs.push_back(cute);
}
}
ll size_rs = rs.size();
ll size_gs = gs.size();
ll size_bs = bs.size();
// 全て偶数
if (size_rs % 2 == 0 && size_gs % 2 == 0) {
cout << 0 << endl;
return 0;
}
// 2つ奇数
vector<ll> even_vec;
vector<ll> odd1_vec;
vector<ll> odd2_vec;
if (size_rs % 2 == 0) {
even_vec = rs;
odd1_vec = gs;
odd2_vec = bs;
}
if (size_gs % 2 == 0) {
even_vec = gs;
odd1_vec = rs;
odd2_vec = bs;
}
if (size_bs % 2 == 0) {
even_vec = bs;
odd1_vec = rs;
odd2_vec = gs;
}
sort(even_vec.begin(), even_vec.end());
sort(odd1_vec.begin(), odd1_vec.end());
sort(odd2_vec.begin(), odd2_vec.end());
ll cur_mini;
cur_mini = dis(odd1_vec, odd2_vec);
if (even_vec.size() == 0) {
cout << cur_mini << endl;
return 0;
}
ll oe1_cur_mini;
oe1_cur_mini = dis(odd1_vec, even_vec);
ll oe2_cur_mini;
oe2_cur_mini = dis(odd2_vec, even_vec);
ll ans;
ans = min(cur_mini, oe1_cur_mini + oe2_cur_mini);
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
ll modpower(ll a, ll r, ll mod){ //a^r
ll x = 1;
if (r < 0) r = r%(mod-1)+mod-1;
a %= mod;
while (r > 0) {
if (r & 1) x = x * a % mod;
r >>= 1;
a = a * a % mod;
}
return x;
}
ll modinv(ll a, ll mod) {
//a*ret%mod == gcd(a, mod)
ll x = 0, y = 1, u = 1, v = 0, b = mod;
while (b != 0) {
ll q = a/b;
a %= b;
swap(a, b);
u -= q*x;
swap(u, x);
v -= q*y;
swap(y, v);
}
if (u > 0) return u;
else return u+mod;
}
vector<ll> fact;
vector<ll> invfact;
void set_fact(ll n, ll mod){
fact.resize(n+1, 1);
invfact.resize(n+1, 1);
for (ll i = 2; i <= n; i++) {
fact[i] = fact[i-1] * i % mod;
}
invfact[n] = modinv(fact[n], mod);
for (ll i = n-1; i >= 2; i--) {
invfact[i] = invfact[i+1] * (i+1) % mod;
}
return;
}
ll comb(ll n, ll k, ll mod) {
if (k > n || k < 0) return 0;
if (k == n || k == 0) return 1;
return fact[n] * invfact[n-k] % mod * invfact[k] % mod;
}
int main() {
ll N, M;
cin >> N >> M;
ll mod = 998244353;
set_fact(N+M+10, mod);
vector<vector<ll>> dp(M+1, vector<ll>(21, 0));
for (ll i = M; i >= 2; i--) {
dp[i][1] = 1;
for (ll j = 2; j <= 20; j++) {
for (ll k = i*2; k <= M; k += i) {
dp[i][j] += dp[k][j-1];
if (dp[i][j] >= mod) dp[i][j] -= mod;
}
}
}
ll ans = 1;
for (ll i = 2; i <= M; i++) {
for (ll j = 1; j <= 20; j++) {
ans = (ans+dp[i][j]*comb(N, j, mod))%mod;
}
}
cout << ans << 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;
} const int N = 2e5 + 5, mod = 998244353;
int qpow (int x, int y) {
int t = 1;
while (y) {
if (y & 1) t = t * x % mod;
x = x * x % mod, y >>= 1;
}
return t;
}
int n, m, res, a[N], t, p[N * 2], in[N * 2], b[N], tot, k[N];
int C (int x, int y) {
return p[x] * in[y] % mod * in[x - y] % mod;
}
void prework () {
p[0] = 1;
for (int i = 1; i <= n + m; ++i) p[i] = p[i - 1] * i % mod;
in[n + m] = qpow (p[n + m], mod - 2);
for (int i = n + m; i >= 1; --i) in[i - 1] = in[i] * i % mod;
for (int i = 2; i * i <= m; ++i)
if (!k[i]) {
b[++tot] = i;
for (int j = i; j * j <= m; j += i) k[j] = 1;
}
}
void work (int x) {
t = 0;
for (int i = 1; i <= tot && b[i] <= x; ++i) {
int cnt = 0;
while (x % b[i] == 0) ++cnt, x /= b[i];
if (cnt) a[++t] = cnt;
}
if (x > 1) a[++t] = 1; int tmp = 1;
for (int i = 1; i <= t; ++i) {
tmp = tmp * C (a[i] + n - 1, a[i]) % mod;
}
// printf ("%lld %lld %lld\n", x, tmp, b[1]);
(res += tmp) %= mod;
}
signed main() {
read (n), read (m); prework ();
// printf ("%d\n", tot);
for (int i = 1; i <= m; ++i) work (i);
printf ("%lld\n", res);
}
|
#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define FORR(i,a,b) for (int i = b - 1; i >= a; --i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#define DEBUG
#ifdef DEBUG
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << " in " << __FILE__ << ")" << COLOR_RESET << endl
#else
#define dbg(x) (x)
#endif
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int A, B, C;
cin >> A >> B >> C;
if (A * A + B * B < C * C) cout << "Yes" << endl;
else cout << "No" << endl;
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;
string s;
int main()
{
int n,x;
cin>>n>>x>>s;
rep(i,n)
{
if(s[i]=='x') x=max(x-1,0);
else x++;
}
cout<<x<<endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll modq=998244353;
const ll INF=1e18;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}
ll modpow(ll a,ll n) {
ll res = 1;
while (n>0) {
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
int main(void){
vector<ll> Fib(1050);
Fib.at(0)=0,Fib.at(1)=1;
rep(i,1010) Fib.at(i+2)=(Fib.at(i+1)+Fib.at(i))%mod;
ll N;
cin>>N;
char caa,cab,cba,cbb;
cin>>caa>>cab>>cba>>cbb;
if(N<4){
cout<<1<<endl;
return 0;
}
if(caa=='A' && cab=='A'){
cout<<1<<endl;
return 0;
}
if(cbb=='B' && cab=='B'){
cout<<1<<endl;
return 0;
}
if(cab=='B' && cbb=='A'){
if(cba=='A'){
cout<<modpow(2,N-3)<<endl;
return 0;
}
cout<<Fib.at(N-1)<<endl;
return 0;
}
if(cba=='B'){
cout<<modpow(2,N-3)<<endl;
return 0;
}
cout<<Fib.at(N-1)<<endl;
return 0;
} | #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;
}
template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
T 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;
void solve()
{
int n;
cin>>n;
vector<ll>a(n),b(n);
rep(i,n)cin>>a[i];
rep(i,n)cin>>b[i];
ll maxv=0;
ll pre=0;
rep(i,n){
ll res=0;
chmax(maxv,a[i]);
res=b[i]*maxv;
chmax(res,pre);
cout<<res<<"\n";
pre=res;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define ft first
#define sec second
#define pr pair<int,int>
#define ISCC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int t ,n ,m ,a ,sum;
string s;
int main()
{
cin >> n;
for(int i=0 ;i<n ;i++)
{
cin >> a;
sum += max(0 ,a-10);
}
cout << sum;
return 0;
} | /*** -God give me the power − ***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void test()
{
int n;
cin>>n;
vector <ll> arr(n);
map <ll,int> mp;
ll ans=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
mp[arr[i]]++;
}
for(int i=0;i<n;i++)
{
mp[arr[i]]--;
ans+=max(0,n-i-1-mp[arr[i]]);
}
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin>>t;
while(t--)
{
test();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int main()
{
int A, B, W;
cin >> A >> B >> W;
W *= 1000;
int mini = INT_MAX, maxi = 0;
if (W%A==0) {
maxi = W / A;
}
if (W%B==0) {
mini = W / B;
}
if (maxi>0&&mini>0) {
doublece(mini,maxi)
}
else {
if (W/A==W/B) {
ce("UNSATISFIABLE")
}
else {
doublece(W / B+1, W / A)
}
}
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int A, B, W;
cin >> A >> B >> W;
W *= 1000;
int most = 0;
int least = 0;
// most = W / A + (W % A == 0 ? 1 : 0);
// least = W / B + (W % B == 0 ? 0 : 1);
if (W % A == 0)
most = W / A;
else
{
int n = W / A;
float avg = float(W) / n;
if (avg > A && avg < B)
most = n;
}
if (W % B == 0)
least = W / B;
else
{
int n = W / B + 1;
float avg = float(W) / n;
if (avg > A && avg < B)
least = n;
}
if (most == 0 || least == 0)
cout << "UNSATISFIABLE" << endl;
else
cout << least << " " << most << endl;
// cout << least << " " << most << endl;
}
|
#define _USE_MAT_DEFINES
#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <stack>
#include <deque>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
#define rep(i,n) for(long long (i)=0;(i)<(n);(i)++)
#define FOR(i, m, n) for(long long (i) = (m);(i) < (n);(i)++)
#define INF 1e9
const long long MOD = 1000000007;//1e9+7
int main() {
ll X, Y, A, B;
cin >> X >> Y >> A >> B;
ll ans = 0;
while ((double)A*X <= 2e18 &&X*A<X+B&&X*A<Y) {
X *= A;
ans++;
}
ans += (Y - 1 - X) / B;
cout << ans;
return 0;
} | #include<bits/stdc++.h>
typedef long long int lli;
typedef long double lld;
typedef long long ll;
//Fast Input I/O
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL);
//Datatype
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define vvlli vector<vector<long long int>>
#define ppi pair<int, int>
#define rppi pair<int, pair<int, int>>
#define lppi pair<pair<lli, lli>, lli>
#define vppi vector<pair<int, int>>
#define sppi stack<pair<int int>>
#define qppi queue<pair<int, int>>
//function
#define f first
#define s second
#define pb(x) push_back(x)
#define mkp(i, j) make_pair(i, j)
#define lmkp(i,j,k) make_pair(make_pair(i,j),k)
#define rmkp(i,j,k) make_pair(i,make_pair(j,k))
//loop
#define loop(i,n) for (i = 0; i < n; ++i)
#define loops(i,k,n) for (i = k; i <= n; ++i)
#define looprev(i,k,n) for (i = k; i >= n; --i)
//Const
#define inf (int)1e9
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
//Print
#define prd(n) printf("%d", n)
#define prl(n) printf("%lld", n)
#define prdn(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define prf(n) printf("%f", n)
//Scan
#define scd(n) scanf("%d", &n)
#define scd2(a, b) scanf("%d %d", &a, &b)
#define scd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scl(n) scanf("%lld", &n)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define scf(n) scanf("%f", &n)
#define all(x) (x).begin(), (x).end()
using namespace std;
void online_judge() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve() {
lli i, j, k, n;
cin >> n;
vector<lppi> A;
loop(i, n) {
lli t, l, r;
cin >> t >> l >> r;
A.push_back(lmkp(l, r, t));
}
sort(A.begin(), A.end());
lli res = 0;
loop(i, n) {
lli l = A[i].f.f, r = A[i].f.s, t = A[i].s;
loops(j, i + 1, n - 1) {
if (A[j].f.f < r || (A[j].f.f == r && (t == 1 || t == 3) && (A[j].s == 1 || A[j].s == 2))) {
res += 1;
}
}
}
cout << res << "\n";
}
int main() {
fio;
online_judge();
lli t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define ff(i,n) for(i=0;i<n;i++)
//ncr(n,r) = ncr(n-1,r-1) + ncr(n-1,r)
const ll bg = 1e9+7;
void solve(){
ll i,n,x;
cin>>n>>x;
x = x*100;
ll ans = -1,sum=0;
ll v[n],p[n];
ff(i,n){
cin>>v[i]>>p[i];
}
ff(i,n){
sum += v[i]*p[i];
if(sum>x)
{
ans = i+1;
break;
}
}
cout<<ans;
}
int main(){
ll t;
t=1;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define VVI vector<vector<int>>
#define PII pair<int,int>
#define VPII vector<PII>
#define LD long double
#define INF 1000000000000
#define MOD 1000000007
//#define MAXR 100000 // array max range
#define MAXR 200000+10
#define FI first
#define SE second
#define PQG priority_queue<int,VI,greater<int>>
#define PQL priority_queue<int,VI,less<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; }
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n;
int p[MAXR],mem[MAXR];
int m=0,mm=0;
R(i,MAXR) mem[i] = false;
cin >> n;
R(i,n){
cin >> p[i];
mem[p[i]] = true;
if(mm==p[i]){
FOR(j,mm+1,MAXR){
if(!mem[j]){
mm=j;
m=j;
break;
}
}
}
cout << m << endl;
}
//int cnt=0;
//R(i,MAXR){
// if(ans[i]){
// cout << i-1 << endl;
// cnt++;
// }
// if(n==cnt){
// break;
// }
//}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
ll n,k;
cin>>n>>k;
ll arr[n][2];
ll sum=0;
ll x,y;
for(ll i=0;i<n;i++){
cin>>x>>y;
arr[i][0]=x;
arr[i][1]=y;
}
int flag=0;
for(ll i=0;i<n;i++){
ll a = arr[i][0];
ll b = arr[i][1];
sum +=a*b;
double z= sum;
if(z/100>k){
flag=i+1;
break;
}
}
if(flag==0)
cout<<"-1"<<endl;
else
cout<<flag<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef unsigned long long lli;
const lli M = 1000000007;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
while(t--)
{
lli n,s,d,flag=0; cin>>n>>s>>d;
for(int i=0; i<n ; i++)
{
int x,y;
cin>>x>>y;
if(x<s && y>d)
{
flag=1;
}
}
if(flag==1)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
pi operator - (pi a, pi b) { return { a.first - b.first, a.second - b.second }; }
pi operator + (pi a, pi b) { return { a.first + b.first, a.second + b.second }; }
ll operator * (pi a, pi b) { return a.first * 1LL * b.first + a.second * 1LL * b.second; }
const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };
// 최대공약수, x, y
vector<ll> ee(ll a, ll b) {
if (b == 0)
return { a, 1, 0 };
ll g, x, y;
vector<ll> t = ee(b, a % b);
g = t[0];
x = t[1];
y = t[2];
return { g, y, x - (a / b) * y };
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
ll x, y, p, q;
cin >> x >> y >> p >> q;
ll mn = 9e18;
for (int t1 = 0; t1 < y; ++t1)
for (int t2 = 0; t2 < q; ++t2) {
ll a = (2 * x + 2 * y), b = -(p + q);
ll c = p - x + t2 - t1;
vector<ll> res = ee(a, b);
if (c % res[0] != 0)
continue;
ll u = res[1] * c / res[0];
ll k = u - u * res[0] / b * b / res[0];
while (u != 0 && k - abs(u * b / res[0]) >= 0)
k -= abs(b / res[0]);
while (u != 0 && k < 0)
k += abs(b / res[0]);
mn = min(mn, a * k + x + t1);
}
if (mn == 9e18)
cout << "infinity\n";
else
cout << mn << "\n";
}
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#define rep(i,begin, end) for (ll i = begin; i < (ll)(end); i++)
using namespace std;
using ll = long long;
template<typename T>
using vec2 = vector<vector<T>>;
template<typename T>
using vec3 = vec2<vector<T>>;
template<typename T>
using vec4 = vec3<vector<T>>;
template<typename T>
using vec5 = vec4<vector<T>>;
template<typename T>
using vec6 = vec5<vector<T>>;
ll extGCD(ll a, ll b, ll &x, ll &y){
if(b == 0){
x = 1;
y = 0;
return a;
}
ll g = extGCD(b, a%b, y, x);
y -= a/b * x;
return g;
}
ll MOD = 1000000007;
vector<ll> mfact(1,1);
ll minv(ll a){
ll x;ll y;
extGCD(a, MOD, x, y);
if(x<0){
x+= ((-x)/MOD+1)*MOD;
x %= MOD;
}
return x;
}
ll mpow(ll a,ll b){
ll res = 1;
while(b > 0){
if(b%2==1)res=(res*a)%MOD;
b/=2;
a = (a*a)%MOD;
}
return res;
}
ll mfactorial(ll a){
if(mfact.size()-1 >= a)return mfact[a];
for(ll i = mfact.size();i<=a;i++){
mfact.push_back((i * mfact[i-1])%MOD);
}
return mfact[a];
}
ll mcomb(ll n, ll r){
return (((mfactorial(n) * minv(mfactorial(r)))%MOD) * minv(mfactorial(n-r)))%MOD;
}
ll ipow(ll a, ll b){
ll res = 1;
while(b > 0){
if(b % 2 == 1)res*=a;
a*=a;
b/=2;
}
return res;
}
void factorize(map<ll,ll>& fac, vector<ll>& sieved, ll a){
while(a != 1){
if(fac.find(sieved[a]) == fac.end())fac[sieved[a]] = 0;
fac[sieved[a]]++;
a/=sieved[a];
}
}
void primesieve(vector<ll>& res,ll n){
res = vector<ll>(n+1, -1);
for(int i = 2;i <= n;i++){
if(res[i] != -1)continue;
res[i] = i;
for(int j = 2;j*i <= n;j++){
res[j*i] = i;
}
}
}
vector<ll> primesuntil(ll n){
vector<ll> res;
vector<ll> sieved;
primesieve(sieved, n);
for(int i = 2;i <= n;i++){
if(sieved[i] == i)res.push_back(i);
}
return res;
}
int gcd(int a, int b){
if(b == 0)return a;
return gcd(b, a%b);
}
void solve(){
// cout<<"##########"<<endl;
ll X,Y,P,Q;cin>>X>>Y>>P>>Q;
ll res = -1;
rep(q,0,Q){
rep(y,0,Y){
ll d = gcd(2*X+2*Y, -(P+Q));
if((P+q-X-y) % d != 0)continue;
// cout<<(P+q-X-y)<<endl;
// cout<<d<<endl;
ll s,t;
extGCD((2*X+2*Y)/d, -(P+Q)/d, s,t);
// cout<<s<<" "<<t<<endl;
s *= (P+q-X-y)/d;
t *= (P+q-X-y)/d;
ll k = d<0 ? floor(min(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)))
: ceil(max(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)));
// cout<<k<<endl;
s += k*(P+Q)/d;
t += k*(2*X+2*Y)/d;
// cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
// cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
ll a = (2*X+2*Y)*s+X+y;
if(res == -1)res = a;
res = min(res,a);
// cout<<a<<endl;
// cout<<"!!!!!!!!!!!!!!!!"<<endl;
}
}
cout<<(res==-1 ? "infinity" : to_string(res))<<endl;
}
int main(){
int T;cin>>T;
rep(i,0,T)solve();
}
|
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>
#define sqr(x) (x) * (x)
typedef long long int i64;
using namespace std;
using namespace std::chrono;
const i64 mod = 1'000'000'000ll + 7;
//const i64 mod = 998'244'353ll;
const i64 inf = 1'000'000'000'000'000'000ll / 1'000'000'000ll;
const long double eps = 1e-8;
i64 gcd(i64 a, i64 b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main(int argc, char* argv[]) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);
if (sizeof(i64) != sizeof(long long int)) {
cerr << "i64 != long long int" << endl;
}
i64 R = 0;
i64 n;
string s;
cin >> n >> s;
i64 cnt = 0;
stringstream ss;
while (cnt < n) {
ss << "110";
cnt += 3;
}
ss << "110";
string t = ss.str();
for (i64 l = 0; l < 3; l++) {
if (t.substr(l, s.size()) != s) {
continue;
}
const i64 m = 30'000'000'000ll - n - l;
R += m / 3 + 1;
}
cout << R << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
string ss="",str;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
long long n;
cin>>n;
cin>>str;
for(int i=0;i<=n+100;i++)
if(i%3==0)ss+="1";
else if(i%3==1)ss+="1";
else ss+="0";
if(str=="1")cout<<20000000000<<"\n";
else if(ss.substr(0,n)==str){
int p=(n-1)/3;
cout<<10000000000-p<<"\n";
}
else if(ss.substr(1,n)==str){
int p=n/3;
cout<<10000000000-p<<"\n";
}
else if(ss.substr(2,n)==str){
int p=(n+1)/3;
cout<<10000000000-p<<"\n";
}
else cout<<0<<"\n";
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> II;
inline II make_key(int a, int b) {
return make_pair(min(a, b), max(a, b));
}
int solve(int N, int M, std::vector<int> &A, std::vector<int> &B, std::vector<char> &C) {
vector<vector<int>> v(26);
for (int i = 0; i < M; i++) {
v[C[i] - 'a'].emplace_back(i);
}
map<II, set<II>> m;
set<II> adj;
for (int ch = 0; ch < 26; ++ch) {
for (int i = 0; i < v[ch].size(); ++i) {
int a = A[v[ch][i]], c = B[v[ch][i]];
adj.emplace(make_key(a, c));
for (int j = i + 1; j < v[ch].size(); ++j) {
int b = A[v[ch][j]], d = B[v[ch][j]];
m[make_key(a, b)].insert(make_key(c, d));
m[make_key(a, d)].insert(make_key(b, c));
m[make_key(b, c)].insert(make_key(a, d));
m[make_key(c, d)].insert(make_key(a, b));
}
}
}
set<II> q;
q.emplace(II(0, N - 1));
for (int i = 0; i < M && !q.empty(); ++i) {
set<II> nq;
for (auto x : q) {
if (adj.find(x) != adj.end()) {
return i * 2 + 1;
}
for (auto y : m[x]) {
if (y.first == y.second) {
return (i + 1) * 2;
}
nq.insert(y);
}
}
q = nq;
}
return -1;
}
int main() {
int N, M;
std::cin >> N >> M;
std::vector<int> A(M), B(M);
std::vector<char> C(M);
for (int i = 0; i < M; i++) {
std::cin >> A[i] >> B[i] >> C[i];
--A[i], --B[i];
}
cout << solve(N, M, A, B, C) << endl;
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;}
vector<pci> v[1000];
int dist[1000][1000][27];
/*
int go(int o, int oo, int al) {
if (o == oo) return 0;
if (dp[o][oo][al] != -1) return dp[o][oo][al];
int miv = 1e9;
return dp[o][oo][al];
}*/
struct node {
int o, oo, al;
};
queue<node> q;
int main() {
memset(dist, -1, sizeof(dist));
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int a,b;
char c;
scanf("%d %d %c", &a, &b, &c);
a--; b--;
c -= 'a';
v[a].push_back({c, b});
v[b].push_back({c, a});
}
q.push({0,n-1,26});
dist[0][n-1][26] = 0;
while (!q.empty()) {
node tv = q.front();
q.pop();
int o = tv.o, oo = tv.oo, al = tv.al, co = dist[o][oo][al];
if (o == oo) return !printf("%d", co);
if (al == 26) {
for (pci i : v[o]) {
if (dist[i.second][oo][i.first] == -1) {
dist[i.second][oo][i.first] = co + 1;
q.push({i.second, oo, i.first});
}
}
}
else {
for (pci i : v[oo]) {
if (i.first == al && dist[o][i.second][26] == -1) {
dist[o][i.second][26] = co + 1;
q.push({o, i.second, 26});
}
}
}
}
printf("-1");
}
// author: rdd6584
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i;
char x[1000];
cin>>x;
char *dot_pos = strchr(x, '.');
if (dot_pos) *dot_pos = '\0';
puts(x);
cout<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
const long long INFll = 8223372036854775807;
const int INFii = 1147483647;
const long long mod = (long long) 1e9 + 7;
typedef long long ll;
typedef int ii;
typedef double dbl;
#define endl '\n'
#define S second
#define F first
#define MP make_pair
#define PB push_back
#define maxn 110
string x;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> x;
for(ii i = 0; i < x.size(); i++) {
if(x[i] == '.') break;
cout << x[i];
}
cout << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mp make_pair
#define fr first
#define sc second
int gcd(int x,int y){
if(y==0)return x;
return gcd(y,x%y);
}
int main(){
int n,ret=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
int a;
scanf("%d",&a);
ret=gcd(ret,a);
}
cout<<ret<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265358979
#define EPS 1e-8
#define mod 1000000007
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl "\n"
#define NEG_INF -2e18
const ll INF=1e15;
const int N=4e5+50;
ll fac[N];
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll modinverse(ll a,ll m)
{
return binpow(a,m-2,m);
}
ll nck(ll n,ll r)
{
return ((fac[n]*modinverse(fac[r],mod)) % mod)*modinverse(fac[n-r],mod)%mod;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(0);
ll n;
cin>>n;
vector<ll> v(n);
for(ll i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end());
ll g=v[0];
for(ll i=1;i<n;i++)
{
g=__gcd(g,v[i]-v[0]);
}
cout<<g;
}
|
/* -*- coding: utf-8 -*-
*
* f.cc: F - GCD or MIN
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 2000;
const int INF = 1 << 30;
/* typedef */
typedef pair<int,int> pii;
typedef unordered_map<int,int> mii;
/* global variables */
int as[MAX_N];
mii mp;
/* subroutines */
template <typename T>
T gcd(T m, T n) { // m > 0, n > 0
if (m < n) swap(m, n);
while (n > 0) {
T r = m % n;
m = n;
n = r;
}
return m;
}
/* main */
int main() {
int n;
scanf("%d", &n);
int mina = INF;
for (int i = 0; i < n; i++) {
scanf("%d", as + i);
mina = min(mina, as[i]);
}
//printf("mina=%d\n", mina);
for (int i = 0; i < n; i++) {
int ai = as[i];
for (int p = 1; p <= mina && p * p <= ai; p++)
if (ai % p == 0) {
mii::iterator mit = mp.find(p);
if (mit == mp.end()) mp.insert(pii(p, ai));
else mit->second = gcd<int>(mit->second, ai);
int q = ai / p;
if (q <= mina) {
mii::iterator mit = mp.find(q);
if (mit == mp.end()) mp.insert(pii(q, ai));
else mit->second = gcd<int>(mit->second, ai);
}
}
}
int c = 0;
for (mii::iterator mit = mp.begin(); mit != mp.end(); mit++)
if (mit->first == mit->second) c++;
printf("%d\n", c);
return 0;
}
| #include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream>
#include<string>
#include<map>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f,maxn=60,MOD=998244353;
#define ll long long
//vector<int>v[maxn];
//map<int,string>mp[maxn];
//queue<int>q[maxn];
int a[maxn][maxn],n,K,siz[maxn],fa[maxn],ans;
int quick_read_num()
{
int x=0,flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+(c-'0');
c=getchar();
}
return flag*x;
}
void Init()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=Find(fa[x]);
}
void Add(int x,int y)
{
x=Find(x),y=Find(y);
if(x==y)
return;
if(x<y)
fa[x]=y;
else
fa[y]=x;
}
ll jc(int x)
{
if(x<2)
return 1;
ll mul=1;
for(int i=2;i<=x;++i)
{
mul*=i;
mul%=MOD;
}
return mul;
}
void _gather1()
{
int i,j;
for(i=1;i<=n;++i)
{
//vis[i]=false;
for(j=i+1;j<=n;++j)
{
bool flag=true;
for(int k=1;k<=n;++k)
if(a[i][k]+a[j][k]>K)
{
flag=false;
break;
}
if(flag)
{
// cout<<i<<" "<<j<<endl;
Add(i,j);
}
}
}
}
void _gather2()
{
int i,j;
for(i=1;i<=n;++i)
{
//vis[i]=false;
for(j=i+1;j<=n;++j)
{
bool flag=true;
for(int k=1;k<=n;++k)
if(a[k][i]+a[k][j]>K)
{
flag=false;
break;
}
if(flag)
{
// cout<<i<<" "<<j<<endl;
Add(i,j);
}
}
}
}
void ans_count()
{
int i,j,si=0;
for(i=1;i<=n;i++)
siz[Find(fa[i])]++;
for(i=1;i<=n;i++)
{
// cout<<siz[i]<<endl;
ans=(ans*jc(siz[i]))%MOD;
}
}
int main()
{
int i,j,k;
ans=1ll;
n=quick_read_num();
K=quick_read_num();
//cout<<n<<" "<<k<<endl;
Init();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=quick_read_num();
_gather1();
ans_count();
Init();
memset(siz,0,sizeof(siz));
_gather2();
ans_count();
cout<<ans;
return 0;
}
|
#define taskname "test"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 105;
const int mod = 1e9 + 7;
int powmod(int x, int k) {
if(k == 0) return 1;
int t = powmod(x, k / 2);
if(k % 2 == 0) return t * 1LL * t % mod;
else return t * 1LL * t % mod * x % mod;
}
int inv(int x) {
return powmod(x, mod - 2);
}
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
struct matrix {
int val[maxn][maxn];
matrix() {
fill_n(&val[0][0], sizeof(val) / sizeof(val[0][0]), 0);
}
matrix operator *(const matrix&other) const {
matrix res;
for(int i = 0; i < maxn; ++i) {
for(int j = 0; j < maxn; ++j) {
for(int k = 0; k < maxn; ++k) {
res.val[i][j] = add(res.val[i][j], val[i][k] * 1LL * other.val[k][j] % mod);
}
}
}
return res;
}
};
matrix mpow(matrix x, int k) {
if(k == 1) return x;
matrix t = mpow(x, k / 2);
if(k % 2 == 0) return t * t;
else return t * t * x;
}
int n, m, k;
int a[maxn];
vector<int> gr[maxn];
void read_input() {
cin >> n >> m >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
gr[u].push_back(v);
gr[v].push_back(u);
}
}
void solve() {
if(k == 0) {
for(int i = 1; i <= n; ++i) {
cout << a[i] << '\n';
}
return;
}
matrix A, B;
for(int j = 1; j <= n; ++j) {
A.val[1][j] = a[j];
}
for(int j = 1; j <= n; ++j) {
for(auto&i: gr[j]) {
B.val[i][j] = inv(m) * 1LL * inv(2) % mod;
}
B.val[j][j] = add(inv(m) * 1LL * inv(2) % mod * sz(gr[j]) % mod, (m - sz(gr[j])) * 1LL * inv(m) % mod);
}
A = A * mpow(B, k);
for(int i = 1; i <= n; ++i) {
cout << A.val[1][i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read_input();
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
vi matmul(vi dp,vvi mt,int n){
vi res(n);
rep(i,n)rep(j,n)res[i]+=mt[i][j]*dp[j]%mod;
rep(i,n)res[i]%=mod;
return res;
}
vvi update(vvi mt,int n){
vvi res(n,vi(n));
rep(i,n)rep(j,n)rep(k,n)res[i][j]+=mt[i][k]*mt[k][j]%mod;
rep(i,n)rep(j,n)res[i][j]%=mod;
return res;
}
void matpow(vi &dp,vvi mt,ll k,int n){
while(k){
if(k&1)dp=matmul(dp,mt,n);
mt=update(mt,n);
k>>=1;
}
}
/*
int main(){
ll n,k;cin>>n>>k; //nは分かってる項数、kは求めたいのが何項目か
vi dp(n);
rep(i,n)cin>>dp[n-i-1];
if(k<=n)dame(dp[n-k]); //分かっている
vvi mt(n,vi(n));
rep(i,n)cin>>mt[0][i]; //遷移の式
REP(i,1,n)mt[i][i-1]=1; //こうすると上手くいく
matpow(dp,mt,k-n,n);
out(dp[0]);
}
*/
//ABC009D 漸化式
int main(){
ll n,m,k;cin>>n>>m>>k;
vvi mt(n,vi(n));
vi dp(n);rep(i,n)cin>>dp[i];
ll tmp=modpow(m,mod-2);
ll tmp2=modpow(2,mod-2);
rep(i,m){
ll a,b;cin>>a>>b;a--;b--;
mt[a][b]=tmp2;
mt[b][a]=tmp2;
}
rep(i,n){
ll sum=0;
rep(j,n)sum+=mt[i][j];
mt[i][i]=(m+mod-sum%mod)%mod;
}
rep(i,n)rep(j,n)mt[i][j]=mt[i][j]*tmp%mod;
matpow(dp,mt,k,n);
for(ll x:dp)out(x);
} |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> edges[26];
vector<pair<int, int>> states[1024][1024];
int d[1024][1024];
int main()
{
int N, M;
scanf("%d %d", &N, &M);
queue<pair<int, int>> q;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
d[i][j] = 1'000'000'000;
}
d[i][i] = 0; q.emplace(i, i);
}
for (int i = 0; i < M; i++) {
int u, v; char c[2];
scanf("%d %d %s", &u, &v, c);
if (u > v) swap(u, v);
edges[c[0] - 'a'].emplace_back(u, v);
if (u != v) d[u][v] = d[v][u] = 1;
q.emplace(u, v);
}
for (int a = 0; a < 26; a++) {
int sz = edges[a].size();
for (int i = 0; i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
auto [u, v] = edges[a][i];
auto [w, x] = edges[a][j];
states[u][w].emplace_back(v, x); states[w][u].emplace_back(v, x);
states[u][x].emplace_back(v, w); states[x][u].emplace_back(v, w);
states[v][w].emplace_back(u, x); states[w][v].emplace_back(u, x);
states[v][x].emplace_back(u, w); states[x][v].emplace_back(u, w);
}
}
}
while (q.size()) {
auto [u, v] = q.front(); q.pop();
if (u > v) swap(u, v);
if (u == 1 && v == N) {
printf("%d\n", d[u][v]); return 0;
}
for (auto [u2, v2] : states[u][v]) {
if (d[u2][v2] == 1'000'000'000) {
d[u2][v2] = d[v2][u2] = d[u][v] + 2;
q.emplace(u2, v2);
}
}
}
printf("-1\n");
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
int vdnmcjdkmkh;
cin>>n>>m;
vector<pair<int,int>>a(m);
for(int i=0;i<m;i++)
{
cin>>a[i].first>>a[i].second;
}
int k;
cin>>k;
vector<pair<int,int>>b(k);
for(int j=0;j<k;j++)
{
cin>>b[j].first>>b[j].second;
}
ll l=0;
ll tot=1<<k;
for(ll mask=0;mask<tot;mask++)
{
vector<int>v(n+1);
ll ans=0;
for(int i=0;i<k;i++)
{
if(mask & (1<<i))
v[b[i].second]=1;
else
v[b[i].first]=1;
}
for(int i=0;i<m;i++)
{
if(v[a[i].first] && v[a[i].second])
ans++;
}
l=max(l,ans);
}
cout<<l<<endl;
return 0;
} |
#include <bits/stdc++.h>
#include <variant>
#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif
using namespace std;
using namespace std::string_literals;
using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
//using namespace harudake;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
i32 n,m;
cin>>n>>m;
vector<vi32> g(n);
rep(i,m) {
i32 a,b;
cin>>a>>b;
--a;--b;
g[a].push_back(b);
}
i32 ans = 0;
rep(i,n) {
vector<bool> vis(n, false);
queue<i32> q;
q.push(i);
vis[i] = true;
while (!q.empty()) {
i32 j = q.front();
q.pop();
for (auto&& to : g[j]) {
if (!vis[to]) {
q.push(to);
vis[to] = true;
}
}
}
ans += count(all(vis), true);
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;
const int N = 2222;
vector<int> graph[N];
bool vis[N];
int n, m, ans;
void dfs(int u) {
if(vis[u]) return;
vis[u] = true;
ans++;
rall(v, graph[u]) {
dfs(v);
}
}
int solve() {
cin >> n >> m;
rep(i, 1, m) {
int u, v;
cin >> u >> v;
graph[u].pb(v);
}
rep(u, 1, n) {
fill(vis + 1, vis + n + 1, 0);
dfs(u);
}
cout << ans << endl;
return 0;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while(t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n=1; cin >> n ;
int ans = 0 ;
if (n%100) {
ans++ ;
n -= n%100 ;
}
n /= 100 ;
ans += n%10 ;
n /= 10 ;
ans += n*10 ;
cout << ans << endl ;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
#define Rep(i,n) for(ll i=0;i<ll(n);i++)
#define Repd(i,n) for(ll i=n-1;i>=0;i--)
#define For(i,a,b) for(ll i=a;i<=ll(b);i++)
#define Ford(i,a,b) for(ll i=a;i>=ll(b);i--)
#define Fora(i,I) for(const auto& i:I)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define pb push_back
const int INF = 1<<30;
const ll MOD = 1000000007;
const ll val = 0;
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}
int main(void) {
ll n;
cin>>n;
cout<<myceil(n , 100)<<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;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MODADD(ADD_X,ADD_Y) (ADD_X+ADD_Y)%MOD
#define MODSUB(SUB_X,SUB_Y) ((SUB_X-SUB_Y)+MOD)%MOD
#define MODMUL(MUL_X,MUL_Y) (MUL_X*MUL_Y)%MOD
#define LCM(LCM_X,LCM_Y) (LCM_X/__gcd(LCM_X,LCM_Y))*LCM_Y
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e5+500;
int dx[]= {1,0,-1,0,1,-1,1,-1};
int dy[]= {0,1,0,-1,1,-1,-1,1};
int arr[MAX],mp[MAX];
void solve(int a){
for(int i=0;i<MAX;i++){
if(i-a>0 && mp[i-a])
mp[i]=1;
}
mp[a]=1;
}
int main()
{
FastRead
//freopen("output.txt", "w", stdout);
int n,sum=0;
cin>>n;
for(int i=0; i<n; i++){
int a;
cin>>a;
solve(a);
sum+=a;
}
int mx=INT_MAX;
for(int i=0;i<MAX;i++){
if(mp[i]){
int x=sum-i;
mx=min(mx,max(x,i));
}
}
cout<<mx<<endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int T,n,ans=1e9;
bitset<100004>f;
int main(){
cin>>n;
f[0]=1;
for(int i=0;i<n;i++){
int x;cin>>x;
f|=f<<x;T+=x;
}
for(int i=0;i<=1e5;i++)if(f[i])ans=min(ans,max(i,T-i));
cout<<ans;
} |
//Saptak_Roy_Turja
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ll n,i,x;
cin>>n;
for(i=1;;i++){
x=(i*(i+1))/2;
if(x>=n){
cout<<i<<'\n';
return 0;
}
}
}
| // Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ll long long
#define int long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl "\n"
#define x first
#define y second
#define gcd(a,b) __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define hell 1000000007
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}
int solve(){
int n;cin>>n;
int ans=0;
for(int i=1;i<=1e6;i++){
int dig = log10(i)+1;
int val = pow(10,dig)*i+i;
if(val<=n)ans++;
}
cout<<ans<<endl;
return 0;
}
signed main(){
ios_base::sync_with_stdio(0);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;
int main(){
char s, t;
cin >> s >> t;
t = (s == 'Y' ? t - 32 : t);
cout << t << endl;
return 0;
} | #include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <functional>
#include <iomanip>
#include <cstdint>
#define ll long long
#define rep2(i,a,b) for(int i=a;i<=b;++i)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep3(i,a,b) for(int i=a;i>=b;i--)
#define REP(e,v) for(auto e:v)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tpiii tuple<int,int,int>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector
#define vi vec<int>
#define vl vec<ll>
#define vvi vec<vi>
#define vvl vec<vl>
#define vpii vec<pii>
#define vpll vec<pll>
#define vbl vec<bool>
#define endl "\n"
#define ALL(c) (c).begin(),(c).end()
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}
string stin(){string s;cin>>s;return s;}
void drop(string s){cout<<s<<endl; exit(0);}
void drop(int x){cout<<x<<endl; exit(0);}
void drop(ll x){cout<<x<<endl; exit(0);}
void drop(double x){cout<<x<<endl; exit(0);}
int main(){
char c;
cin>>c;
char a;
cin>>a;
if(c=='Y')cout<<(char)(a+'A'-'a')<<endl;
else cout<<a<<endl;
} |
#include "bits/stdc++.h"
//#include <boost/multi_array.hpp>
//#include <boost/optional.hpp>
//#include <boost/range/irange.hpp>
//#include <boost/range/algorithm.hpp>
//#include <boost/range/adaptors.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
cww() {
ios::sync_with_stdio(false);
cin.tie(0);
}
} star;
const long long INF = numeric_limits<long long>::max();
long long gcd(long long a, long long b) { // 2つの場合の最大公約数
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
long long gcd_vec(vector<long long> const &A) { // N個の要素に対する最大公約数
int size = (int)A.size();
long long ret = A[0];
for (int i = 1; i < size; i++) {
ret = gcd(ret, A[i]);
}
return ret;
}
void Main() { //う し た ぷ に き あ く ん 笑
int n;
cin >> n;
vector<long long> a(n);
rep(i, n) { cin >> a[i]; }
cout << gcd_vec(a) << endl;
} //う し た ぷ に き あ く ん 笑
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 1e9
#define INFL 1e18
#define mod 1000000007
//#define mod 998244353
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;}
ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//cout<< fixed << setprecision(10)
//__builtin_popcountll()
int main(){
fast;
int n,i,j;
cin>>n;
int a[n];
set<int>s;
for(i=0;i<n;i++)
{
cin>>a[i];
s.insert(a[i]);
}
int x;
while(s.size()>1)
{
set<int>p;
set<int>::iterator it;
for(it=s.begin();it!=s.end();it++)
{
if(it==s.begin())
{
x=*it;
p.insert(x);
}
else
{
if(*it%x!=0)
p.insert(*it%x);
}
}
s=p;
}
cout<<*s.begin();
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
int main(){
int n, m;
cin >> n >> m;
vector<vi> d(n, vi(n));
rep(i, m) {
int a, b;
cin >> a >> b;
--a;
--b;
d[a][b] = d[b][a] = 1;
}
vi dp(1<<n, INF);
dp[0] = 0;
rep(i, n) dp[1<<i] = 1;
rep(mask, 1<< n) if(dp[mask] != INF) {
rep(i, n) {
if(mask & (1<<i)) continue;
bool f = true;
rep(j, n) if(mask & (1<<j)) {
f &= d[i][j];
}
if(f) dp[mask | (1<<i)] = 1;
}
}
rep(mask, 1<<n) {
for(int maskA = mask; maskA != 0; maskA = mask&(maskA-1)) {
int maskB = mask - maskA;
dp[mask] = min(dp[mask], dp[maskA] + dp[maskB]);
}
}
cout << dp[(1<<n)-1] << '\n';
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5+8, inf = 1e18+9, mod = 998244353;
struct edge { int v, w; };
int n, m, ans[maxn], vs[maxn];
vector<edge> G[maxn];
void dfs(int u, int p) {
for (auto [v, w] : G[u]) if (v != p && !vs[v]) {
if (w == ans[u]) ans[v] = (w % n) + 1; else ans[v] = w;
vs[v] = 1; dfs(v, u);
}
}
void solve() {
int i, j, u, v, w;
cin >> n >> m;
for (i = 1; i <= m; i++) {
cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
ans[1] = 1; vs[1] = 1;
dfs(1, -1);
for (i = 1; i <= n; i++) cout << ans[i] << endl;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> Pair;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
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;}
VI exist(100100,0),vis,C,ans;
VVI edge;
void dfs(int v){
vis[v] = 1;
bool flag = false;
if(exist[C[v]] == 0){
flag = true;
exist[C[v]] = 1;
ans.push_back(v+1);
}
for(int to : edge[v]){
if(vis[to]) continue;
dfs(to);
}
if(flag) exist[C[v]] = 0;
return;
}
void Main(){
int N; cin >> N;
C.resize(N); vis.resize(N,0); edge.resize(N);
REP(i,N) cin >> C[i], C[i]--;
REP(_,N-1){
int a,b; cin >> a >> b; a--; b--;
edge[a].push_back(b); edge[b].push_back(a);
}
dfs(0);
SORT(ans);
for(int x : ans) cout << x << en;
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
while(t--) Main();
return 0;
} | // MOHIT KUMAR
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pin;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
#define ff(i, a, b) for ( ll i=a; i<(b); i++)
#define ffd(i, a, b) for ( ll i=a; i<= b; i++)
#define fr(i, a, b) for ( ll i = (b)-1; i >= a; i--)
#define ffr(i, a, b) for ( ll i = b ; i >= a; i--)
#define vl vector < ll >
#define vp vector < pl >
#define endl "\n"
#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const ld pi = acos(-1) ;
const ll maxn = 3e5 + 5 ;
const int mod = 998244353 ;
const ll INF = 1e18 ;
const int MX = 2000000001 ; // check the limits, dummy
vl lps(maxn, 0) ; // longest prefix array which is also a suffix
void KMP( string s) {
ll l = 0, r = 1 ;
while (r < s.size()) {
if (s[l] == s[r]) {
lps[r++] = ++l ;
}
else {
l = 0 ;
lps[r++] = 0;
}
}
ff(i, 0, sz(s)) cout << lps[i] << " " ;
}
void find_sub(string s, string t) {
ll i = 0, j = 0, ans = 0 ;
bool ch = false ;
while (i < s.size()) {
if (s[i] == t[j]) {
j++ ;
i++ ;
if (j == t.size()) {
ans++ ;
cout << "Substring found at " << i - j << " index" << endl ;
// return true ;
j = lps[j - 1] ;
ch = true ;
}
}
else {
if (j) j = lps[j - 1] ;
else i++ ;
}
}
if (!ch) cout << "Substring not found\n" ;
cout << "NUMBER OD OCC" << ans << endl ;
// return false ;
}
ll max_gold_mine() {
ll v[3][1] = {{1}, {2}, {3}};
ll m = 3, n = 1 ;
ll ans = 0 ;
for ( int j = n - 2; j >= 0; j--) {
for ( int i = 0; i < m; i++) {
if (!i) v[i][j] += max(v[i][j + 1], v[i + 1][j + 1]) ;
else if (i == m - 1)v[i][j] += max(v[i][j + 1], v[i - 1][j + 1]) ;
else v[i][j] += max({v[i + 1][j + 1], v[i - 1][j + 1], v[i][j + 1]}) ;
}
}
for ( int i = 0; i < m; i++) ans = max(ans, v[i][0]) ;
return ans ;
}
int optimal_merge( vector < int > v) {
int ans = 0 ;
priority_queue < int , vector < int > , greater < int >> pq;
for (auto x : v) pq.push(x) ;
while (pq.size() != 1) {
int a = pq.top() ;
pq.pop() ;
int b = pq.top() ;
pq.pop() ;
ans += a + b ;
pq.push(a + b) ;
}
return ans ;
}
// void dfs( ll u) {
// vis[u] = 1 ;
// for (auto x : adj[u]) {
// if (!vis[x]) dfs(x) ;
// }
// top.pb(u) ;
// }
// void topo_sort(vl adj[], ll n) {
// ffr(i, 1, n) {
// if (!vis[i]) dfs(i) ;
// }
// reverse(all(top)) ;
// cout << "TOPOLOGICAL SORTING OF GRAPH IS\n " ;
// for (auto x : top) cout << x << " ";
// }
// vl v[maxn] ;
// vector <double > prob(maxn, 0) ;
// void dfs (ll n, ll par) {
// ll res = sz(v[n]) ;
// if (par != -1) --res ;
// for (auto x : v[n]) {
// if ( x != par ) {
// prob[x] = prob[n] / res ;
// dfs(x, n) ;
// }
// }
// }
vl v[maxn], val(maxn, 0) ;
bool vis[maxn] = {0} ;
ll csum = 0 ;
void dfs(ll n ) {
vis[n] = 1 ;
csum += val[n] ;
for (auto x : v[n]) {
if (!vis[x]) dfs(x) ;
}
}
int main() {
ios_base::sync_with_stdio(0) , cin.tie(0);
ll t, q, n, a, b, c, d, k, l, m, r, x = 0, y = 0, z = 0 , sum = 0, ans = 0, temp = 0, res = 0 ;
cin >> n >> m ;
ff(i, 0, n) {
cin >> val[i] ;
}
ff(i, 0, n) {
cin >> x ;
val[i] -= x ;
}
ff(i, 0, m) {
cin >> a >> b ;
a--, b-- ;
v[a].pb(b) ;
v[b].pb(a) ;
}
ff(i, 0, n) {
if (!vis[i])
dfs(i) ;
if (csum) {
cout << "No\n" ;
return 0 ;
}
}
cout << "Yes\n" ;
}
|
#include <bits/stdc++.h>
using namespace std;
const auto mod = 1000000007;
auto fp(long long x, long long e) -> long long {
auto ans = 1ll;
while (e > 0) {
if (e & 1) ans = ans * x % mod;
e >>= 1;
x = x * x % mod;
}
return ans;
}
auto fib(int n) -> long long {
if (n < 2) return 1ll;
n--;
auto Fi_1 = 1ll, Fi = 1ll;
while (n-- > 0) {
auto tmp = Fi;
Fi = (Fi_1 + Fi) % mod;
Fi_1 = tmp;
}
return Fi;
}
auto main() -> int {
auto n = 0;
auto AA = char{}, AB = char{},
BA = char{}, BB = char{};
cin >> n;
cin >> AA >> AB >> BA >> BB;
if (n <= 3) {
cout << 1 << endl;
return 0;
}
if (AB == 'A') {
if (AA == 'A') {
cout << 1 << endl;
} else if (BA == 'B') {
cout << fp(2, n - 3) << endl;
} else {
cout << fib(n - 2) << endl;
}
} else {
if (BB == 'B') {
cout << 1 << endl;
} else if (BA == 'A') {
cout << fp(2, n - 3) << endl;
} else {
cout << fib(n - 2) << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,l,r) for (int i=l;i<=r;++i)
#define per(i,r,l) for (int i=r;i>=l;--i)
#define ll long long
using namespace std;
const int N=1e6+6,P=1e9+7; int dp[N];
int main(){
int n; scanf("%d",&n);
char str[4][5],s[4];
rep(i,0,3) scanf("%s",str[i]),s[i]=str[i][0];
if (s[1]=='A'&&s[0]=='A'||s[1]=='B'&&s[3]=='B'){
puts("1"); return 0;
}
if (s[1]==s[2]){
dp[1]=dp[2]=1;
rep(i,3,n-1) dp[i]=(dp[i-1]+dp[i-2])%P;
cout<<dp[n-1]<<endl;
}
else{
int s=1; rep(i,1,n-3) s=2LL*s%P;
printf("%d\n",s); return 0;
}
return 0;
} |
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
long long int n,j=0,sum=0,i=0,s=0;
cin>>n;
int a[n];
while(i<n)
{
cin>>a[i];
i++;
}
sort(a,a+n);
while(j<n)
{
sum =sum+a[j]*j;
sum =sum- s;
s =s+ a[j];
j++;
}
cout<<sum;
} | #include <bits/stdc++.h>
#include <bits/extc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
struct chash {
const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count();
static unsigned long long hash_f(unsigned long long x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; }
int operator()(int x) const { return hash_f(x)^RANDOM; }
};
const int N = 1e10+5;
gp_hash_table<int,int,chash> bit[2];
void update(int idx, int pos, int val){
pos+=(int)1e9;
while(pos < N){
bit[idx][pos] += val;
pos += pos&-pos;
}
}
int query(int idx, int pos){
pos+=(int)1e9;
int res = 0;
while(pos){
res += bit[idx][pos];
pos -= pos&-pos;
}
return res;
}
void solve(){
int n;
cin >> n;
int ans = 0;
for(int i = 0;i < n;i++){
int tmp;
cin >> tmp;
ans += (query(0,N-1)-query(0,tmp))-(query(1,N-1)-query(1,tmp))*tmp-query(0,tmp-1)+query(1,tmp-1)*tmp;
update(0,tmp,tmp);
update(1,tmp,1);
}
cout << ans << "\n";
}
signed main(){
fastio
int t = 1;
//cin >> t;
while(t--) solve();
} |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int n, a[maxn], b[maxn], c[maxn][maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &c[i][j]);
}
}
int mn = min_element(c[1] + 1, c[1] + n + 1) - c[1];
for (int i = 1; i <= n; i++) {
b[i] = c[1][i] - c[1][mn];
}
memset(a, -1, sizeof(a));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int t = c[i][j] - b[j];
if (t < 0) puts("No"), exit(0);
if (~a[i]) {
if (a[i] ^ t) puts("No"), exit(0);
} else {
a[i] = t;
}
}
}
puts("Yes");
for (int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
putchar('\n');
for (int i = 1; i <= n; i++) {
printf("%d ", b[i]);
}
putchar('\n');
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define deb(x) cout << #x << " " << x << endl;
#define mod 1000000007
#define fast std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
const ll INF = 1e18;
const ll NEGINF = -1 * INF;
void solve()
{
int n;
cin >> n;
ll x, t;
ll a = 0;
ll b = INT_MIN;
ll c = INT_MAX;
for (int i = 0; i < n; i++)
{
cin >> x >> t;
if (t == 1)
{
a += x;
b += x;
c += x;
}
else if (t == 2)
{
b = max(b, x);
c = max(c, x);
}
else
{
b = min(b, x);
c = min(c, x);
}
}
// deb(a);
// deb(b);
// deb(c);
int q;
cin >> q;
while (q--)
{
cin >> x;
cout << min(c, max(b, a + x)) << endl;
}
}
int main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/input.txt", "r", stdin);
freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/output.txt", "w", stdout);
#endif
int T = 1;
while (T--)
{
solve();
}
//cout<< "Done in " << clock() / CLOCKS_PER_SEC <<"sec"<< endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#define manx maxn
#define whlie while
#define itn int
#define fro for
#define asn ans
#define scnaf scanf
#define sacnf scanf
#define pritnf printf
#define haed head
#define tmep temp
#define udpate update
#define cosnt const
#define Fastin freopen("/home/rqdmap/Desktop/codes/in", "r", stdin)
#define Fastout freopen("/home/rqdmap/Desktop/codes/main.out", "w", stdout)
#define lowbit(x) ((x) & (-x))
#define clr(a, x) memset((a), x, sizeof (a))
#define TTT int __; scanf("%d", &__); for(int _ = 1; _ <= __; _++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
/*------------------------------------------------------------------------------------*/
template<typename T>
void prt(T *a, int n, bool withid = 0){
if(!withid) for(int i = 0; i < n; i++) cout << a[i] << ((i == n - 1)? '\n':' ');
else for(int i = 0; i < n; i++) cout << i << ": " << a[i] << '\n';
}
template<typename T>
T ceil(T a, T b){return (a + b - 1) / b;}
template<typename T>
T gcd(T x, T y){return !x? y: gcd(y % x, x);}
template<typename T>
void exgcd(T a, T b, T &x, T &y){
if(!b){x = 1; y = 0;return;}
exgcd(b, a % b, y, x); y -= a / b * x;
}
ll qp(ll a, ll p){
if(p < 0) return 0;
ll ans = 1; while(p){if(p & 1) ans = ans * a ; a = a * a; p >>= 1;}
return ans;
}
ll qp(ll a, ll p, ll M){
ll ans = 1; while(p){ if(p & 1) ans = ans * a % M; a = a * a % M; p >>= 1;}
return (ans % M + M) % M;
}
int euler(int *prime, int *vis, int n){
int top = 0;
for(int i = 0; i <= n; i++) vis[i] = 0;
for(int i = 2; i <= n; i++){
if(!vis[i]) prime[top++] = i;
for(int j = 0; j < top && i * prime[j] <= n; j++){
vis[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
return top;
}
struct star{int to, next, w;};
/*------------------------------------------------------------------------------------*/
const int maxn = 5e3 + 10;
const int M = 998244353;
ll C(int n, int m){
m = min(m, n - m);
ll ans = 1;
for(int i = 1; i <= m; i++){
ans = ans * qp(i, M - 2, M) % M;
ans = ans * (n + 1 - i) % M;
}
return ans;
}
ll dp[maxn];
ll c[maxn];
int main(){
// Fastin;
int n, m; scnaf("%d %d", &n, &m);
for(int i = 0; i <= n; i++) c[i] = C(n, i);
dp[0] = 1;
for(int i = 1; i <= m; i++){
if(i & 1) dp[i] = 0;
else{
for(int j = 0; j + j <= i && j + j <= n; j++){
dp[i] += c[2 * j] * dp[i / 2 - j] % M;
dp[i] %= M;
}
}
}
// prt(dp, m + 1, 1);
printf("%lld\n", dp[m]);
return 0;
} | #line 1 "main.cpp"
/**
* @title Template
*/
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
class range {
struct iter {
std::size_t itr;
constexpr iter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { ++itr; }
constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
struct reviter {
std::size_t itr;
constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { --itr; }
constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
const iter first, last;
public:
constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
constexpr iter begin() const noexcept { return first; }
constexpr iter end() const noexcept { return last; }
constexpr reviter rbegin() const noexcept { return reviter(*last - 1); }
constexpr reviter rend() const noexcept { return reviter(*first - 1); }
};
/**
* @title Range
*/
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"
#include <cstddef>
#line 7 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"
#include <iterator>
#line 9 "/Users/kodamankod/Desktop/cpp_programming/Library/algebraic/fact_prime.cpp"
#include <type_traits>
template <class T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
std::vector<std::pair<T, size_t>> factorize(T x) {
assert(x > 0);
if (x == 1) return { };
std::vector<std::pair<T, size_t>> res;
for (T i = 2; i * i <= x; ++i) {
if (x % i == 0) {
res.emplace_back(i, 0);
while (x % i == 0) {
res.back().second++;
x /= i;
}
}
}
if (x > 1) {
res.emplace_back(x, 1);
}
return res;
}
template <class T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
std::vector<T> divisors(const T x) {
std::vector<T> small, big;
for (T i = 1; i * i <= x; ++i) {
if (x % i == 0) {
small.push_back(i);
if (i * i != x) {
big.push_back(x / i);
}
}
}
small.reserve(small.size() + big.size());
std::copy(big.rbegin(), big.rend(), std::back_inserter(small));
return small;
}
/**
* @title Factors/Divisors
*/
#line 16 "main.cpp"
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
constexpr i32 inf32 = (u32) ~0 >> 2;
constexpr i64 inf64 = (u64) ~0 >> 2;
template <class T>
using Vec = std::vector<T>;
int main() {
u64 D;
std::cin >> D;
u64 ans = 0;
for (const auto x: divisors(2 * D)) {
if (x & 1) {
ans += 2;
}
}
std::cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}
//main関数だよーーー
int main() {
ll K,N,M;
cin>>K>>N>>M;
vector<ll> ans(K),p(K);
rep(i,K) cin>>p[i];
vector<pair<ll,ll>> q(K);
ll X=0;
rep(i,K){
ans[i]=(M*p[i])/N;
q[i]={(M*p[i])%N,i};
X+=q[i].first;
}
assert(X%N==0);
Sore(q);
X/=N;
rep(i,X){
ans[q[i].second]++;
}
rep(i,K){
cout<<ans[i];
if(i!=K-1) cout<<" ";
else cout<<"\n";
}
}
| #include<bits/stdc++.h>
using namespace std;
long long x,y,n,m,k,ans,mm[200002],nn[200002],ff[200002],nm;
vector<int>sz[200002];
void add(int a){while(a<=nm)ff[a]++,a+=a&(-a);}
int ask(int a){int ans=0;while(a)ans+=ff[a],a-=a&(-a);return ans;}
int main(){
scanf("%lld%lld%lld",&n,&m,&k);nm=max(n, m);
for(int i=1;i<=nm;++i)mm[i]=m+1,nn[i]=n+1;
for(int i=1;i<=k;++i){scanf("%lld%lld",&x,&y);mm[x]=min(mm[x],y);nn[y]=min(nn[y],x);}
for(int i=1;i<mm[1];++i)ans+=nn[i]-1,sz[nn[i]].push_back(i);
for(int i=mm[1];i<=m;++i)sz[1].push_back(i);
for(int i=1;i<nn[1];++i){for(int j:sz[i])add(j);ans+=ask(mm[i]-1);}
printf("%lld",ans);
return 0;
} |
Subsets and Splits