code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
const int N=2e+5+10;
vector<int> adj[N];
pair<int,int> edj[N];
int n,lft[N],rgt[N],t;
long long int c[N];
void dfs(int v,int p)
{
lft[v]=++t;
for(auto c:adj[v])
{
if(c!=p)
{
dfs(c,v);
}
}
rgt[v]=t;
}
int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
cin>>edj[i].first>>edj[i].second;
adj[edj[i].first].push_back(edj[i].second);
adj[edj[i].second].push_back(edj[i].first);
}
t=0;
dfs(1,0);
int q;
cin>>q;
while(q--)
{
int t,i;
long long int x;
cin>>t>>i>>x;
int a=edj[i].first,b=edj[i].second;
i = b;
if(lft[a]>lft[b])
{
i=a;
t=3-t;
}
if(t==1)
{
//i=edj[i].second;
c[1]+=x;
c[lft[i]]-=x;
c[rgt[i]+1]+=x;
c[n+1]-=x;
}
else
{
//i=edj[i].second;
c[lft[i]]+=x;
c[rgt[i]+1]-=x;
}
}
for(int i=1;i<=n;i++)
{
//cout<<lft[i]<<" "<<rgt[i]<<" ";
c[i]+=c[i-1];
//cout<<c[i]<<"\n";
}
for(int i=1;i<=n;i++)
cout<<c[lft[i]]<<"\n";
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)
ll INF = 1ll << 60;
int main(){
int n;
cin >> n;
vl a(n-1), b(n-1);
vvl g(n, vl(0));
rep(i, 0, n-1){
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
vl dps(n,-1);
dps[0] = 0;
queue<ll> que;
que.push(0);
while(!que.empty()){
ll d = que.front();
que.pop();
for(ll i : g[d]){
if(dps[i] == -1){
dps[i] = dps[d]+1;
que.push(i);
}
}
}
int q;
cin >> q;
vl sum(n);
rep(i, 0, q){
ll t, e, x;
cin >> t >> e >> x;
e--;
ll ae, be;
ae = a[e];
be = b[e];
if(dps[ae] > dps[be]){
t ^= 3;
swap(ae, be);
}
if(t == 1){
sum[0] += x;
sum[be] -= x;
}
else{
sum[be] += x;
}
}
que.push(0);
while(!que.empty()){
ll d = que.front();
que.pop();
for(ll i : g[d]){
if(dps[i] > dps[d]){
sum[i] += sum[d];
que.push(i);
}
}
}
rep(i, 0, n)
cout << sum[i] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define inf 1000000000000000018
int mod = 1e9 + 7;
int po(int x, int y, int m) { if (y == 0) return 1; int t = po(x, y / 2, m) % m; t = (t * t) % m; if (y & 1) return (t * x) % m; return t % m; }
void fun() {
vector<vector<int>> ncr(61, vector<int> (61));
for (int r = 1; r < 61; ++r)
ncr[0][r] = 0;
ncr[0][0] = 1;
for (int n = 1; n < 61; ++n) {
ncr[n][0] = 1;
for (int r = 1; r < 61; ++r) {
ncr[n][r] = ncr[n - 1][r] + ncr[n - 1][r - 1];
}
ncr[n][n] = 1;
}
// cout << ncr[4][3];
int a, b, k;
cin >> a >> b >> k;
int n = (a + b);
for (int i = 0; i < n ; ++i) {
int cnt = ncr[a - 1 + b][b];
// cout << (a - 1 + b) << " " << b << " " << cnt << endl;
if (cnt >= k) {
cout << 'a';
a -= 1;
}
else {
cout << 'b';
b -= 1;
k -= cnt;
}
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
fun();
}
// cout << ncr[6][2];
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF INT_MAX
#define FIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
#define mod 1000000007ll
#define forn(i,n) for(int i=0;i<n;i++)
#define forab(i,a,b) for(int i=a;i<=b;i++)
#define forba(i,b,a) for(int i=b;i>=a;i--)
#define debx(x) cout << #x << ": " << x << endl
#define debxy(x,y) cout << #x << ": " << x << ' ' << #y << ": " << y << endl
#define YESp cout << "YES" << endl
#define NOp cout << "NO" << endl
#define Yesp cout << "Yes" << endl
#define Nop cout << "No" << endl
#define write(a) cout << a << endl
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pq priority_queue
ll fast_exp(ll base, ll power){ll ans=1;while(power){if(power&1)ans=(ans*base)%mod;base=(base*base)%mod;power=power>>1;}return ans%mod;}
ll modInv(ll a,ll p){return fast_exp(a,p-2)%p;}
bool is_prime(int n){if(n==1){return false;}int i=2;while(i*i<=n){if(n%i==0){return false;}i+=1;}return true;}
ll gcd(ll a,ll b){if(a==0)return b;return gcd(b%a,a);}
ll modFact(ll n){if(n>=mod)return 0;ll result=1;for(ll i = 1; i <= n; i++)result=(result*i)%mod;return result;}
pii moves[] = {{-1, 0}, {1, 0}, {0 ,-1}, {0, 1}};
/****** S ****** X ****** T ****** Y ****** A ****** M ******/
ll ncr(ll n, ll r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
// gcd of p, k
long long m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else
p = 1;
return p;
}
int main() {
FIO
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", sto");
ll a, b, k;
cin >> a >> b >> k;
string ans = "";
while(a > 0 || b > 0) {
if(k == 1) {
forn(i, a) ans += 'a';
forn(i, b) ans += 'b';
a = 0;
b = 0;
break;
}
ll mn = 1, mx = ncr(a+b-1, a-1);
if(k <= mx) {
ans += 'a';
a--;
} else {
ans += 'b';
b--;
k -= mx;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < n; i++)
#define rep(i,a,n) for (int i = a; i < n; i++)
#define SORT(s) sort(s.begin(), s.end())
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
int m = pow(2,n);
vi a(m/2), b(m/2);
REP(i,m) {
if(i < m/2) cin >> a[i];
else cin >> b[i-m/2];
}
int amax = 0, bmax = 0, a1, b1;
REP(i,m/2) {
if (amax < a[i]) {
amax = a[i];
a1 = i+1;
}
if (bmax < b[i]) {
bmax = b[i];
b1 = i+m/2+1;
}
}
cout << (amax < bmax ? a1 : b1) << endl;
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int n;
cin>>n;
int a[(1<<n)];
int maxi = -1,ind;
for(int i=0;i<(1<<n);i++) {
cin>>a[i];
if(maxi<a[i]){
maxi=a[i];
ind=i;
}
}
if(ind >= (1<<(n-1)) ){
maxi=-1;
for(int i=0;i<(1<<(n-1));i++){
if(maxi<a[i]){
maxi=a[i];
ind=i;
}
}
cout<<ind+1<<endl;
}
else{
maxi=-1;
for(int i=(1<<(n-1));i<(1<<n);i++){
if(maxi<a[i]){
maxi=a[i];
ind=i;
}
}
cout<<ind+1<<endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using vi = vector<int64_t>;
using vvi = vector<vi>;
using pii = pair<int64_t, int64_t>;
using vvp = vector<vector<pii>>;
int64_t INF = 1e18;
int64_t mod = 1e9+7;
int main() {
int x;
cin >> x;
x = max(0, x);
cout << x << endl;
}
| #pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const long double Pi = 3.141592653;
const ll mod=1e9+7;
long long INF = 1000000000000000000;
bool cmp(pair<int,int>a,pair<int,int>b)
{
return (a.first<b.first) || (a.first==b.first && a.second<b.second);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t=1;//cin>>t;
while(t--)
{
string x;cin>>x;
for(int i=0;i<x.length();i++)
{
if(x[i]!='.')
cout<<x[i];
else
break;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
int n, a;
string s;
cin >> n >> a >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'x') {
if (a > 0) {
a--;
}
} else {
a++;
}
}
cout << a << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1, i = 1;
//cin >> t;
while(t--) {
//cout << "Case #" << i << ": ";
solve();
//i++;
}
return 0;
}
| #include<bits/stdc++.h>
#define rep(i, n) for(int i=0;i<n;++i)
using namespace std;
typedef long long ll;
int main() {
int n;
double d, h;
cin >> n >> d >> h;
double max_h = 0;
rep(i, n) {
double x, y;
cin >> x >> y;
max_h = max(max_h, (d * y - h * x) / (d - x));
}
cout << setprecision(10) << max_h << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ui unsigned int
#define us unsigned short
#define inf_int 1e9
#define inf_ll 1e18
#define mod 1000000007
#define smod 998244353
const int maxN = 1e5 + 5;
ll table[maxN][2]; // + -
ll cnt[maxN][2];
ll mulmod(ll a, ll b){
ll ans = 0;
while(b > 0){
if(b&1) ans = (ans + a) % mod;
b >>= 1;
a = (a+a)%mod;
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n;
cin >> n;
ll data[n];
for(int x=0;x<n;x++){
cin >> data[x];
}
fill(cnt[0], cnt[n], 0);
table[0][0] = data[0];
table[0][1] = 0;
cnt[0][0] = 1;
for(int x=1;x<n;x++){
table[x][0] = (table[x-1][0] + table[x-1][1] + mulmod(cnt[x-1][0]+cnt[x-1][1], data[x])) % mod;
table[x][1] = (table[x-1][0] - mulmod(cnt[x-1][0], data[x])) % mod;
cnt[x][0] = (cnt[x-1][0] + cnt[x-1][1]) % mod;
cnt[x][1] = cnt[x-1][0] % mod;
}
cout << (table[n-1][0] + table[n-1][1] + 2*mod) % mod << "\n";
return 0;
} | #include <cstdio>
#include <algorithm>
#define int long long
const int M = 200005, p = 998244353;
int n, m, f[20][M], fac[M], inv[M], ans;
int Pow(int a, int b) {
int an = 1;
while(b) {
if(b & 1) an = an * a % p;
a = a * a % p;
b >>= 1;
}
return an;
}
int C(int n, int m) {
if(n < m) return 0;
return fac[n] * inv[m] % p * inv[n-m] % p;
}
void init(int n) {
fac[0] = 1;
for(int i = 1; i <= n; i++) fac[i] = fac[i-1] * i % p;
inv[n] = Pow(fac[n], p-2);
for(int i = n; i >= 1; i--) inv[i-1] = inv[i] * i % p;
}
signed main() {
scanf("%lld%lld", &n, &m);
init(n);
for(int i = 1; i <= m; i++) f[1][i] = 1;
for(int i = 1; i <= 18; i++)
for(int j = 1; j <= m; j++)
for(int k = 2*j; k <= m; k += j)
f[i+1][k] = (f[i+1][k] + f[i][j]) % p;
for(int i = 1; i <= std::min(18ll, n); i++)
for(int j = 1; j <= m; j++)
ans = (ans + f[i][j] * C(n-1, i-1)) % p;
printf("%lld", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define rep2(i, s, n) for(long long i=(s);i<(long long)(n);i++)
#define repi(i, n) for(int i=0;i<(int)(n);i++)
#define rep2i(i, s, n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define chmax(s, t) s=max(s,t);
#define chmin(s, t) s=min(s,t);
#define deg2rad(deg) (((deg)/360)*2*M_PI)
#define rad2deg(rad) (((rad)/2/M_PI)*360)
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
const ll INF = (1LL<<60);
const int INFi = (1<<29);
/*素数判定*/
bool is_prime(ll n){
if(n==1) return false;
for(ll i=2;i*i<=n;i++){
if(n%i==0) return false;
}
return true;
}
/*約数列挙*/
vll enum_divisors(ll n){
vll l;
for(ll i=1;i*i<=n;i++){
if(n%i==0){
l.push_back(i);
if(n/i != i) l.push_back(n/i);
}
}
sort(all(l));
return l;
}
/*素因数分解*/
vector<P> prime_factorize(ll n){
vector<P> l;
for(ll i=2;i*i<=n;i++){
if(n%i!=0) continue;
ll e = 0;
while(n%i==0){
e++;
n /= i;
}
l.push_back({i, e});
}
if(n!=1) l.push_back({n, 1});
return l;
}
/*最小公倍数*/
ll lcm(ll a, ll b){
return a*b/__gcd(a,b);
}
/*最大公約数*/
ll gcd(ll a, ll b){
return __gcd(a,b);
}
/*組み合わせ(Combination)*/
const ll CMAX = 1010000;
const ll CMOD = 1e9+7;
ll fac[CMAX], finv[CMAX], inv[CMAX];
// テーブルを作る前処理
void combinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < CMAX; i++) {
fac[i] = fac[i - 1] * i % CMOD;
inv[i] = CMOD - inv[CMOD%i] * (CMOD / i) % CMOD;
finv[i] = finv[i - 1] * inv[i] % CMOD;
}
}
// 二項係数計算
ll comb(ll n, ll k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % CMOD) % CMOD;
}
/*階乗*/
ll factorial(ll n){
const ll FMOD = 1e9+7;
ll ret = n;
for(ll i=n-1;i>0;i--){
ret*=i;
ret%=FMOD;
}
return (ret?ret:1);
}
int main(){
ll a, b, c, d; cin >> a >> b >> c >> d;
cout << (a*d-b*c) << endl;
return 0;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); i++)
#define INF 1000000000
//配列 vector<型> 配列名(要素数, 初期値);
//二次元配列 vector<vector<型>> 配列名(要素数縦,vector<int>(要素数横, 初期値))
//配列のサイズ(二次元縦) 配列名.size(), 二次元横 配列名[0].size()
// 1秒間のforループは10^8くらい
//最大公約数
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
//最小公倍数
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main() {
int V, S, T, D;
cin >> V >> T >> S >> D;
if (V * T > D || V * S < D) {
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define Pr pair<ll, ll>
#define Tp tuple<int, int, int>
using Graph = vector<vector<int>>;
vector<int> col;
vector<int> ans;
vector<int> col2;
// dfs
// s:始点 i:dfs回数 t:始点からの距離
vector<int> vis;
void dfs(Graph &G, int s, int i) {
if (col[col2[s]] == 0)
ans.push_back(s);
col[col2[s]]++;
for (int nx : G[s]) {
if (vis[nx] == i)
continue;
vis[nx] = i;
dfs(G, nx, i);
}
col[col2[s]]--;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
col.assign(100001, 0);
col2.assign(N + 1, 0);
rep(i, N) { cin >> col2[i + 1]; }
Graph G(N + 1);
rep(i, N - 1) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
// main関数内で
vis.assign(N + 1, -1);
vis[1] = 0;
dfs(G, 1, 0); // s:始点 i:dfs回数
sort(ans.begin(), ans.end());
rep(i, ans.size()) { cout << ans[i] << "\n"; }
} | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 998244353
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
void Initpar(long n, vl &par){
//par = vector<int>(n);
for(long i=0; i<n; i++) par.at(i) = i;
}
int root(long x, vl &par){
if(x == par.at(x)) return x;
else return par.at(x) = root(par.at(x), par);
}
void unite(long x, long y, vl &par){
long rx = root(x, par);
long ry = root(y ,par);
if(rx == ry) return ;
if(rx < ry) par.at(ry) = rx;
else par.at(rx) = ry;
}
bool same(long x, long y, vl &par){
return root(x, par) == root(y, par);
}
void update_par(vl &par){
for(long i=0;i<par.size();i++){
root(i, par);
}
}
int main(){
long n,k;
cin >> n >> k;
vvl a(n, vl(n));
rep(i,n){
rep(j,n) cin >> a.at(i).at(j);
}
vl permu(n+300,1);
REP(i,1,n+300){
permu.at(i) = permu.at(i-1)*i % MOD;
}
long ans = 1;
vl rpar(n);
vl cpar(n);
Initpar(n,rpar);
Initpar(n,cpar);
rep(i,n){
REP(j,i+1,n){
bool flag = true;
rep(l,n){
long p = a[i][l] + a[j][l];
if(p > k){
flag = false;
break;
}
}
if(flag){
unite(i,j,rpar);
}
}
}
update_par(rpar);
vl rcnt(n,0);
rep(i,n){
rcnt.at(rpar.at(i))++;
}
rep(i,n){
ans = (ans * permu.at(rcnt.at(i))) % MOD;
}
rep(i,n){
//cout << "01" << i << endl;
//cout << n << endl;
REP(j,i+1,n){
//cout << i << j << endl;
bool flag = true;
rep(l,n){
long p = a[l][i] + a[l][j];
//cout << p << endl;
if(p > k){
flag = false;
break;
}
}
if(flag){
unite(i,j,cpar);
}
}
}
update_par(cpar);
//update_par(cpar);
vl ccnt(n,0);
rep(i,n){
ccnt.at(cpar.at(i))++;
}
rep(i,n){
ans = (ans * permu.at(ccnt.at(i))) % MOD;
}
cout << ans << endl;
if(ans < 0) 3/0;
//rep(i,n) cout << permu.at(i) << endl;
} |
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
vector<int> u;
int N,a,b;
cin>>N;
for(int i=0;i<N;i++){
cin>>a;
v.push_back(a);
}
for(int q=0;q<N;q++){
cin>>b;
u.push_back(b);
}
int result = inner_product(
v.begin(), v.end(),
u.begin(),
0.0f);
std::cout << (result==0?"Yes":"No") << std::endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep(i,a) for(ll i=0; i< a; i++)
#define rep1(i,a) for(ll i = 1; i< a; i++)
#define foi(i, a, b) for(ll i = a; i<b ; i++)
#define fod(i, a, b) for(ll i = a; i>=b ; i--)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define MAX 1000005
#define MOD 1000000007
ll findgcd(ll n,ll m){
return __gcd(n,m);
}
ll findlcm(ll n,ll m){
return (n*m)/findgcd(n,m);
}
ll nodig(ll n){
int ans =0;
while(n!=0){
ans+=1;
n=n/10;
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll n,temp=0,cnt=0,sum=0,ans=0;
cin>>n;
vector<ll> a(n),b(n);
rep(i,n) cin>>a[i];
rep(i,n) cin>>b[i];
rep(i,n){
sum+=(a[i]*b[i]);
}
if(sum==0) cout<<"Yes";
else cout<<"No";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define scd(v) scanf("%d",&v)
#define scld(v) scanf("%ld",&v)
#define sclld(v) scanf("%lld",&v)
#define scs(v) scanf("%s",&v)
#define sclf(v) scanf("%lf",&v)
#define rep(i, j) for(int i = 0;i < j;i++)
#define all(v) v.begin(),v.end()
#define cnt(v,n) count(ALL(v),n)
#define sz(v) v.size()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf int(1e9)
#define ll long long
#define pi 3.14159265358979
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
void solve(){
int a,b;
string s;
cin >> a >> b;
cin >> s;
for(int i = 0;s[i];i++){
if(s[i] == 'x'){
if(b > 0)b--;
}else b++;
}
cout << b << endl;
}
int 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;
const int MX = 1e5 + 5;
int N, M;
long long int odd, even;
string s[MX];
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 0; i < N; i++) cin >> s[i];
for(int i = 0; i < N; i++){
int cnt = 0;
for(char c : s[i]) if(c == '1') cnt++;
if(cnt % 2 == 0) even++;
else odd++;
}
cout << odd * even;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
int T;
cin >> T;
ll tmp;
for (int i = 0; i < T; i++) {
cin >> tmp;
if(tmp%2){
cout << "Odd" << endl;
}else if(tmp%4){
cout << "Same" << endl;
} else{
cout << "Even" << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> v(n, vector<int> (4));
for (int i = 0; i < n; i++) {
v.at(i).at(0) = 500 * (i % 20);
v.at(i).at(1) = 1000 * (i / 20);
v.at(i).at(2) = v.at(i).at(0) + 500;
v.at(i).at(3) = v.at(i).at(1) + 1000;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (j != 3) {
cout << v.at(i).at(j) << " ";
}
else {
cout << v.at(i).at(j) << endl;
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9+7;
int mod_pow(int a,int b,int M = MOD)
{
if(a == 0) return 0;
b %= (M - 1); //M must be prime here
int res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
const int N = 100;
vector<bool> isprime(N,true);
void solve()
{
isprime[1] = isprime[0] = 0;
for(int i=2;i<N;i++)
if(isprime[i])
for(int j=i+i;j<N;j+=i) isprime[j] = 0;
vector<int> pr;
for(int i=2;i<=72;i++) if(isprime[i]) pr.pb(i);
int l,r;
cin >> l >> r;
int n = r - l + 1;
vector<int> a(n);
f(i,n) a[i] = l + i;
vector<int> mask(n,0);
f(i,n)
{
f(j,20) if(a[i] % pr[j] == 0) mask[i] |= (1<<j);
}
vector<int> cur(1<<20,0),nex(1<<20,0);
cur[0] = 1;
f(j,n)
{
f(i,(1<<20))
{
nex[i] = 0;
}
f(i,(1<<20))
{
nex[i] = cur[i];
}
f(i,(1<<20))
{
if((i & mask[j]) == 0) nex[i | mask[j]] += cur[i];
}
swap(cur,nex);
}
cout << accumulate(all(cur),0);
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}
| #include<bits/stdc++.h>
using namespace std;
string s;
long long k , diff [300005] , num [300005][20];
long long mod = 1e9 + 7 , dp [200005][3][17] , kk;
map < char , long long > mp;
long long ff ( int x , int on , int dif )
{
if ( dif > 16 ) return 0;
if ( x == s . size () )
{
if ( dif == k ) return 1;
else return 0;
}
if ( dp [x][on][dif] != -1 ) return dp [x][on][dif];
long long ans = 0;
if ( on )
{
int eq = mp [ s [x] ];
for ( int i = 0 ; i <= eq ; i ++ )
{
long long last = diff [ x - 1 ];
if ( x == 0 ) last = 0;
if ( i < eq )
{
if ( num [x][i] == 0 ) ans += ff ( x + 1 , 0 , last + 1 );
else ans += ff ( x + 1 , 0 , last );
ans %= mod;
}
if ( i == eq ) ans += ff ( x + 1 , 1 , diff [x] );
}
ans %= mod;
}
else
{
ans += ff ( x + 1 , 0 , dif ) * dif;
ans += ff ( x + 1 , 0 , dif + 1 ) * ( 16 - dif );
ans %= mod;
}
return dp [x][on][dif] = ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
mp ['0'] = 0;
mp ['1'] = 1;
mp ['2'] = 2;
mp ['3'] = 3;
mp ['4'] = 4;
mp ['5'] = 5;
mp ['6'] = 6;
mp ['7'] = 7;
mp ['8'] = 8;
mp ['9'] = 9;
mp ['A'] = 10;
mp ['B'] = 11;
mp ['C'] = 12;
mp ['D'] = 13;
mp ['E'] = 14;
mp ['F'] = 15;
memset ( dp , -1 , sizeof dp );
cin >> s >> k;
for ( int i = 0 ; i < s . size () ; i ++ )
{
if ( i )
{
for ( int j = 0 ; j <= 16 ; j ++ ) num [i][j] += num [ i - 1 ][j];
}
if ( num [i][ mp [ s [i] ] ] == 0 ) kk ++;
num [i][ mp [ s [i] ] ] ++;
diff [i] = kk;
}
long long add = 0;
add += ff ( 1 , 1 , diff [0] );
for ( int i = 1 ; i < mp [ s [0] ] ; i ++ ) add += ff ( 1 , 0 , 1 );
for ( int i = 1 ; i < s . size () ; i ++ ) add += ff ( i + 1 , 0 , 1 ) * 15;
cout << add % mod;
}
|
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(i=a;i<b;i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define hell 1000000007
using namespace std;
ll pr (ll x , ll y)
{
ll ans=1;
if(y==0) return 1;
while(y>0)
{
if(y%2) ans*=x;
x*=x;
y/=2;
ans%=hell;
x%=hell;
}
return ans;
}
void multiply(ll F[2][2], ll M[2][2]) {
int a = (F[0][0] * M[0][0])%hell + (F[0][1] * M[1][0])%hell;
int b= (F[0][0] * M[0][1])%hell + (F[0][1] * M[1][1])%hell;
int c = (F[1][0] * M[0][0])%hell + (F[1][1] * M[1][0])%hell;
int d = (F[1][0] * M[0][1])%hell+ (F[1][1] * M[1][1])%hell;
a%=hell;
b%=hell;
c%=hell;
d%=hell;
F[0][0] = a;
F[0][1] = b;
F[1][0] = c;
F[1][1] = d;
}
void power(ll F[2][2], ll n) {
if (n == 0 || n == 1)
return;
ll M[2][2] = {{1,1},{1,0}};
power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
multiply(F, M);
}
ll ff(ll n) {
ll F[2][2] = {{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n - 1);
return F[0][0];
}
ll inv ( ll x)
{
return pr(x, hell - 2);
}
void solve()
{
ll n,m,i,j,k,c=0,y=0,ans=0,a=0,b;
string s;
cin>>s>>k;
rep(i,0,k)
{
string smin=s,smax=s;
sort(all(smin));
sort(all(smax));
reverse(all(smin));
int lmax = stoi(smin);
int lmin = stoi(smax);
int aa = lmax - lmin;
//cout<<aa<<endl;
s = to_string(aa);
}
cout<<s;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--)
solve();
}
| #include<bits/stdc++.h>
using namespace std;
#define GODSPEED ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define f first
#define s second
#define newl cout<<"\n";
#define pb push_back
#define mset(a,x) memset(a,x,sizeof(a))
#define debv(a) for(auto it: a)cout<<it<<" ";newl;
#define deb1(a) cout<<a<<"\n";
#define deb2(a,b) cout<<a<<" "<<b<<"\n";
#define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n";
#define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll N = 1e5+5;
const ll mod = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
ll n, k;
void solve(){
cin >> n >> k;
ll ans = 0;
for(int i = 2; i <= 2 * n; i++){
ll sum = k + i;
ll x = i;
if(sum > 2 * n or sum < 2) continue;
if(x > n) x -= 2 * (x - (n + 1));
if(sum > n) sum -= 2 * (sum - (n + 1));
ans += (x - 1) * (sum - 1);
}
deb1(ans)
}
int main(){
GODSPEED;
int test = 1;
//cin >> test;
for(int i = 1; i <= test; i++){
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int a, b, c; cin >> a >> b >> c;
cout << a + b + c - min(a, (min(b, c))) << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
for (int i = 0; i < t; ++i)
{
solve();
}
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#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++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)
void YesNo(bool x) {
if (x) cout << "Yes" << endl;
else cout << "No" << endl;
}
int sum;
int ABCD[4];
bool dfs(int i, int tmp) {
if (tmp*2 == sum) {
return true;
}
if (i+1 >= 4) return false;
else return dfs(i+1, tmp+ABCD[i+1])|dfs(i+1, tmp);
}
int main() {
sum = 0;
rep(i, 4) {
cin >> ABCD[i];
sum+=ABCD[i];
}
YesNo(dfs(-1, 0));
}
|
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include <cstring>
#include<math.h>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdint>
#include <map>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
const int INF = 1 << 29;
const long long mod = 1e9 + 7;
const double pi = 3.141592653589793238;
pair<int, int> mp[201010];
int main() {
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
int min = a;
min = (a > b) ? b : a;
min = (min > c) ? c : min;
cout << sum - min << endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
using ll=int64_t;
using ld=long double;
using ull=unsigned long long;
template <class T>
using grid=vector<vector<T>>;
#define ALL(x) x.begin(),x.end()
#define rep(iter,from,to) for(ll iter=from;iter<to;++iter)
const ll MOD=1e9+7;
const ll INF=1e17;
//#######################################################################
vector<vector<ll>> input(ll N, ll width){
string str;
vector<vector<ll>> vec(N,vector<ll>(width));
for(ll i=0;i<N;++i){
cin>>str;
reverse(ALL(str));
for(ll j=0;j<width;++j){
vec[i][j]=str.back();
str.pop_back();
}
}
return vec;
}
void op(vector<ll> vec){
ll size=(ll)vec.size();
for(ll i=0;i<size-1;++i) cout<<vec[i]<<" ";
cout<<vec.back()<<endl;
}
void op(vector<vector<ll>> vec){
ll height=(ll)vec.size();
ll width=(ll)vec[0].size();
for(ll i=0;i<height;++i) {
for(ll j=0;j<width-1;++j) cout<<vec[i][j]<<" ";
cout<<vec[i].back()<<endl;
}
}
//########################################################################
int solve(){
ll a,b,c;
cin>>a>>b>>c;
if((a==b)) cout<<c<<endl;
else if(a==c) cout<<b<<endl;
else if(b==c) cout<<a<<endl;
else cout<<"0"<<endl;
return 0;
}
int main(void){
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
const ll MOD = 998244353;
struct MLL {
ll x, mod;
MLL(ll y=0, ll m=MOD) { x = y; mod = m; }
MLL &operator+= (const MLL &p) { x = (x+p.x)%mod; return *this; }
MLL &operator-= (const MLL &p) { x = (x-p.x+mod)%mod; return *this; }
MLL &operator*= (const MLL &p) { x = (x*p.x)%mod; return *this; }
MLL &operator/= (const MLL &p) { x = (x*p.inv().x)%mod; return *this; }
MLL operator+ (const MLL &p) const { return MLL(*this)+=p; }
MLL operator- (const MLL &p) const { return MLL(*this)-=p; }
MLL operator* (const MLL &p) const { return MLL(*this)*=p; }
MLL operator/ (const MLL &p) const { return MLL(*this)/=p; }
bool operator== (const MLL &p) const { return x==p.x; }
bool operator!= (const MLL &p) const { return x!=p.x; }
bool operator< (const MLL &p) const { return x< p.x; }
bool operator<= (const MLL &p) const { return x<=p.x; }
bool operator> (const MLL &p) const { return x> p.x; }
bool operator>= (const MLL &p) const { return x>=p.x; }
MLL pow(MLL n) const { MLL result(1), p(x); ll tn = n.x; while(tn){ if (tn&1) result*=p; p*=p; tn>>=1; } return result; }
MLL inv() const { return pow(MOD-2); }
};
MLL operator+ (ll x, MLL p) { return (MLL)x+p; }
MLL operator- (ll x, MLL p) { return (MLL)x-p; }
MLL operator* (ll x, MLL p) { return (MLL)x*p; }
MLL operator/ (ll x, MLL p) { return (MLL)x/p; }
vector<MLL> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x); rep(i,x) fac[i] = i ? fac[i-1]*i : 1; }
MLL nck(MLL n, MLL k) { return fac[n.x]/(fac[k.x]*fac[(n-k).x]); };
ostream &operator<< (ostream &ost, const MLL &p) { return ost << p.x; }
istream &operator>> (istream &ist, MLL &p) { return ist >> p.x; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
int main() {
c_fac();
ll N, M; cin >> N >> M;
MLL dp[21][M+1] = {}; repr(i,1,M+1) dp[1][i] = 1;
repr(i,1,20) repr(j,1,M+1) {
for (ll k=2; j*k<=M; k++) {
dp[i+1][j*k] += dp[i][j];
}
}
// rep(i,20) debug(dp[i],dp[i]+(M+1));
MLL result = 0;
repr(i,1,21) {
if (i>N) break;
MLL sum = 0; repr(j,1,M+1) sum += dp[i][j];
result += sum * nck(N-1,i-1);
}
cout << result << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 998244353;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 200100
ll poww(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
int n, m;
int siv[MAXN];
ll fact[MAXN];
ll comb(int n, int r)
{
return fact[n] * poww(fact[r] * fact[n - r] % mod, mod - 2) % mod;
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
fact[0] = 1;
FOR(i, 1, MAXN) fact[i] = fact[i - 1] * i % mod;
FOR(i, 2, MAXN)
{
if (siv[i]) continue;
for (int j = i; j < MAXN; j += i)
siv[j] = i;
}
ll ans = 1;
FOR(k, 2, m + 1)
{
vector<int> cnt;
int cur = siv[k];
int x = k;
while (x > 1)
{
int c = 0;
while (siv[x] == cur)
{
c++;
x /= cur;
}
cur = siv[x];
cnt.pb(c);
}
ll res = 1;
for (int u : cnt)
{
res = res * comb(u + n - 1, n - 1) % mod;
}
ans = (ans + res) % mod;
}
cout << ans << endl;
return 0;
}
|
// D - Happy Birthday! 2
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
#define rp(i,s,e) for(int i=(s);i<(e);++i)
#define sz(a) int(a.size())
int N;
vi A(200);
vec<vi> B(200);
vec<bool> used(200);
void print(int n, vi v){
cout<< n <<" "; rp(i, 0, n) cout<< v[i] << (i<n-1? " ":"\n");
}
bool dfs(int n, int r, vi C){
if(sz(C) == n){
if(!B[r].size()){ B[r] = C; return false; }
puts("Yes"); print(sz(B[r]), B[r]); print(sz(C), C);
return true;
}
rp(i, C.back(), N){
if(used[i]) continue;
used[i] = true;
C.push_back(i+1);
if(dfs(n, (r + A[i])%200, C)) return true;
C.pop_back();
used[i] = false;
}
return false;
}
int main(){
cin>>N;
for(int&x:A) cin>>x;
rp(n, 1, N+1) rp(i, 0, N) if(dfs(n, A[i]%200, {i+1})) return 0;
puts("No");
}
| #include <bits/stdc++.h>
using namespace std;
const int N=2e2+5;
char w[N][N],s[N],t[N];
void prework(){
w['R']['R']=w['R']['S']=w['S']['R']='R';
w['S']['S']=w['S']['P']=w['P']['S']='S';
w['P']['P']=w['P']['R']=w['R']['P']='P';
}
int main(){
prework();
int n,k;scanf("%d%d%s",&n,&k,s);
while(k--){
strcpy(t,s);strcat(t,s);
for(int i=0;i<n;++i) s[i]=w[t[i*2]][t[i*2+1]];
}
printf("%c\n",s[0]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;
int main(){
double t; ll n; cin>>t>>n;
double r=(100.0*n+t-1)/t;
cout<<(ll)r+(n-1)<<endl;
} | #include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\n'
constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;
constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template<typename T>
bool chmax(T& a, T b){
if(a < b){
a = b;
return true;
}
return false;
}
template<typename T>
bool chmin(T& a, T b){
if(b < a){
a = b;
return true;
}
return false;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
lint t, n;
cin >> t >> n;
lint ans;
for(lint i = 1 ; ; ++i){
lint a = (100 * i) / (100 + t);
lint b = a + 1;
if((100 + t) * a / 100 != i && (100 + t) * b / 100 != i){
ans = i;
break;
}
}
ans += (n - 1) / t * (100 + t);
lint nokori = (n - 1) % t;
while(nokori > 0){
++ans;
lint a = (100 * ans) / (100 + t);
lint b = a + 1;
if((100 + t) * a / 100 != ans && (100 + t) * b / 100 != ans){
--nokori;
}
}
cout << ans << endl;
cout << flush;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair <int,int> Pii;
#define reg register
#define mp make_pair
#define pb push_back
#define Mod1(x) ((x>=P)&&(x-=P))
#define Mod2(x) ((x<0)&&(x+=P))
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); }
template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO;
template <class T=int> T rd(){
T s=0; int f=0;
while(!isdigit(IO=getchar())) f|=IO=='-';
do s=(s<<1)+(s<<3)+(IO^'0');
while(isdigit(IO=getchar()));
return f?-s:s;
}
const int N=2e5+10,INF=1e9+10;
int n;
vector <int> G[N];
int F[N][20],D[N],E[N],L[N];
int ma=-1,id;
void dfs(int u,int f) {
if(D[u]>ma) ma=D[u],id=u;
F[u][0]=f,E[u]=D[u];
rep(i,1,18) F[u][i]=F[F[u][i-1]][i-1];
for(int v:G[u]) if(v!=f) D[v]=D[u]+1,dfs(v,u),cmax(E[u],E[v]);
L[u]=G[u][0]==f?(G[u].size()==1?0:G[u][1]):G[u][0];
sort(G[u].begin(),G[u].end(),[&](int x,int y){ return E[x]<E[y]; });
}
int LCA(int x,int y){
if(D[x]<D[y]) swap(x,y);
for(int del=D[x]-D[y],i=0;(1<<i)<=del;++i) if(del&(1<<i)) x=F[x][i];
if(x==y) return x;
drep(i,18,0) if(F[x][i]!=F[y][i]) x=F[x][i],y=F[y][i];
return F[x][0];
}
int Dis(int x,int y){ return D[x]+D[y]-2*D[LCA(x,y)]; }
int lst;
ll Ans=1e18,A[N],M,B[N];
void dfs2(int u,int f) {
//cout<<u<<' ';
if(!lst) A[lst=u]=1;
else A[u]=A[lst]+Dis(lst,u),lst=u;
M=A[u];
for(int v:G[u]) if(v!=f) dfs2(v,u);
}
void Work(int u){
lst=0;
dfs(u,0),dfs2(u,0);
if(M<Ans) {
Ans=M;
rep(i,1,n) B[i]=A[i];
}
//puts("");
}
int main(){
n=rd();
rep(i,2,n) {
int u=rd(),v=rd();
G[u].pb(v),G[v].pb(u);
}
dfs(1,0),Work(id);
int u=1;
while(L[u]) u=L[u];
Work(u);
while(L[u]) u=L[u];
Work(u);
ll ans=0;
rep(i,1,n) cmax(ans,B[i]);
//printf("%lld\n",ans);
rep(i,1,n) printf("%lld ",B[i]);
}
| #include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <string>
#include <list>
#include <map>
using namespace std;
int min(int a,int b){
if(a>b){
return b;
}else{
return a;
}
}
long long int max(long long int a,long long int b){
if(a>b){
return a;
}else{
return b;
}
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
return modpow(a, mod - 2, mod);
}
void f(vector<vector<int>>& a,vector<vector<int>>& b,int N,int K){
for(int i=0;i<N-1;i++){
for(int j=i+1;j<N;j++){
int l=0;
for(int k=0;k<N;k++){
if(a[k][i]+a[k][j]>K){
break;
}
l+=1;
}
if(l==N){
b[i][j]=1;
b[j][i]=1;
}
}
}
}
void g(vector<vector<int>>& a,vector<vector<int>>& c,int N,int K){
for(int i=0;i<N-1;i++){
for(int j=i+1;j<N;j++){
int l=0;
for(int k=0;k<N;k++){
if(a[i][k]+a[j][k]>K){
break;
}
l+=1;
}
if(l==N){
c[i][j]=1;
c[j][i]=1;
}
}
}
}
int h(vector<vector<int>>& b,vector<int>& x,int N){
int M=1;
vector<int> a(N);
for(int i=0;i<N;i++){
a[i]=0;
}
stack<int> d;
for(int i=0;i<N;i++){
int j=0;
if(a[i]==0){
d.push(i);
a[i]=1;
}
while(!d.empty()){
int k=d.top();
d.pop();
j+=1;
for(int l=0;l<N;l++){
if(b[k][l]==1 && a[l]==0){
d.push(l);
a[l]=1;
}
}
}
if(j>1){
x.push_back(j);
M=max(M,j);
}
}
return M;
}
int main(){
long long int r1,r2,c1,c2;
cin>>r1>>c1>>r2>>c2;
int ans=3;
if(r1==r2 && c1==c2){
ans=min(ans,0);
}
if(r1+c1==r2+c2 || r1-c1==r2-c2 || abs(r1-r2)+abs(c1-c2)<=3){
ans=min(ans,1);
}
if(abs(r1-c1)%2==abs(r2-c2)%2){
ans=min(ans,2);
}
if(r1-r2-c1+c2<=3 && r1-r2-c1+c2>=-3){
ans=min(ans,2);
}
if(-r1+r2-c1+c2<=3 && -r1+r2-c1+c2>=-3){
ans=min(ans,2);
}
if(abs(r1-r2)+abs(c1-c2)<=6){
ans=min(ans,2);
}
cout<<ans<<endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int mask_to_int(string s)
{
int ret = 0;
for (size_t i = 0; i < s.size(); ++i)
ret = (ret * 2) + (s[i] == '1');
return ret;
}
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
a[i] = mask_to_int(s);
}
// cnt[1][x] = number of y differ x in last 10 bits that cnt diff = odd
vector<int> cnt[2];
const int SECOND_HALF = (1 << 10) - 1;
cnt[0].resize(1 << 20);
cnt[1].resize(1 << 20);
for (int x : a) {
int first = x >> 10;
for (int second = 0; second < 1 << 10; ++second) {
int p = __builtin_popcount((x & SECOND_HALF) ^ second) & 1;
++cnt[p][(first << 10) | second];
}
}
long long res = 0;
for (int x : a) {
int second = x & SECOND_HALF;
for (int first = 0; first < 1 << 10; ++first) {
int p = __builtin_popcount((x >> 10) ^ first) & 1;
res += cnt[!p][(first << 10) | second]; // we count pairs with odd diff, hence !p
}
}
cout << res / 2;
return 0;
}
| #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <iostream>
#include <utility>
#include <cstring>
#include <cassert>
#include <vector>
#include <random>
#include <cstdio>
#include <string>
#include <queue>
#include <tuple>
#include <map>
#include <set>
#define MOD 1000000007LL
typedef long long ll;
typedef std::pair < int, int > pii;
int main() {
ll n, k;
scanf("%lld%lld", &n, &k);
for (; k--; ) {
if (n % 200 == 0) n /= 200;
else n = n * 1000 + 200;
}
printf("%lld\n", n);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int h, w, a, b, res;
void dfs(int idx, int bt, int a, int b)
{
if (idx == h * w)
++res;
if (bt & (1 << idx))
dfs(idx + 1, bt, a, b);
if (b)
dfs(idx + 1, bt | (1 << idx), a, b - 1);
if (a)
{
if (!(bt & (1 << (idx + 1))) && idx % w < w - 1)
dfs(idx + 2, bt | (1 << idx) | (1 << (idx + 1)), a - 1, b);
if (idx + w < w * h && !(bt & (1 << (idx + w))))
dfs(idx + 1, bt | (1 << idx) | (1 << (idx + w)), a - 1, b);
}
}
int main()
{
cin.tie(0); ios::sync_with_stdio(false);
cin >> h >> w >> a >> b;
dfs(0, 0, a, b);
cout << res;
} | #include<bits/stdc++.h>
//#include <boost/math/common_factor.hpp>
using namespace std;
// Ordered Set VSCODE M KAM NI KARRA PATANI KIA DARD HAI
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
//#define mp make_pair
#define pii pair<int,int>
//#define int long long
#define min_heap priority_queue <int, vector<int>, greater<int> >
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define MOD 1000000007
//#define modInv(a) binExp(a,mod-2)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test int t;cin>>t;while(t--)
#define nl "\n"
#define ceill(a,b) (a/b)+((a%b)!=0)
//-------------------------IMPORTANT FUNCTIONS START---------------------
int powr(int x, int y)
{
int res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
/*ll power(ll a, ll b, ll mod){
ll x = 1LL, y = a;
while (b > 0LL){
if (b%2LL){
x = (x*y);
if(x>mod) x%=mod;
}
y = (y*y);
if(y>mod) y%=mod;
b /= 2LL;
}
return x;
}*/
bool isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i <=sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
int lcm(int a, int b)
{
return (a / __gcd(a, b)) * b;
}
//-------------------------IMPORTANT FUNCTIONS END---------------------
// a^b=a+b-2*(a&b)
// for xor same hai to 0 different hai to 1
//gcd(a,b)=gcd(a,b-a)
//gcd(a,b,c)=gcd(a,gcd(b,c))=gcd(gcd(a,b),c)=gcd(a,b-a,c)=gcd(a,c,b-a)=gcd(a,c-a,b-a)
// tin tout concept can be used to find the ancestors of node
// if an array size increases from n to n+1 no.of subarray's increases by n+1
// ceil use karna hai to ceill(a,b) kar
// TLE ya MLE aye to ek bar long long hatake try kar
int func(int x,int y,int h,int w,int arr[17][17],int a1,int b1){
int sum=0;
if(y>h){
y=1;
x++;
}
if(x>w)return 1;
if(arr[x][y]==1){
sum=0;
sum+=func(x,y+1,h,w,arr,a1,b1);
return sum;
}
if((x+1)<=w and a1>0){
int cpy[17][17];
loop(i,0,17)loop(j,0,17)cpy[i][j]=arr[i][j];
cpy[x][y]=1;
cpy[x+1][y]=1;
//a1--;
sum+=func(x,y+1,h,w,cpy,a1-1,b1);
}
if(y+1<=h and a1>0){
int cpy[17][17];
loop(i,0,17)loop(j,0,17)cpy[i][j]=arr[i][j];
cpy[x][y]=1;
cpy[x][y+1]=1;
// a1--;
sum+=func(x,y+1,h,w,cpy,a1-1,b1);
}
if(b1>0){
int cpy[17][17];
loop(i,0,17)loop(j,0,17)cpy[i][j]=arr[i][j];
cpy[x][y]=1;
sum+=func(x,y+1,h,w,cpy,a1,b1-1);
}
return sum;
}
void solve(){
int h,w,a1,b1;
cin>>h>>w>>a1>>b1;
int arr[17][17];
memset(arr,0,sizeof(arr));
cout<<func(1,1,h,w,arr,a1,b1);
}
signed main()
{
fast
// cout<<fixed<<setprecision(15);
//test
solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define fill(a,b) memset(a,b,sizeof(a))
#define f first
#define s second
#define pb push_back
#define inf 1e17
#define ninf -1*1e17
#define mod 1000000007
#define N 500005
#define Ns 5005
#define hsum 600000
string s;
ll n,m,o,x,y,z,d,k,l,r;
ll a[N];
ll dp[102][hsum];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t=1;
//cin>>t;
while(t--){
// ll ans=0;
cin>>n>>k>>m;
dp[0][0]=1;
for(int i=0;i<=n;i++){
for(int j=0;j<=hsum;j++){
ll cnt=0;
if(j-i>=0){
cnt=(cnt+dp[i][j-i])%m;
}
if(i-1>=0){
cnt=(cnt+dp[i-1][j])%m;
}
if(i-1>=0 && (j-(k+1)*i) >=0){
cnt=(cnt-dp[i-1][j-(k+1)*i]+m)%m;
}
dp[i][j]=cnt;
}
}
for(int i=1;i<=n;i++){
ll ans=0;
for(int j=1;j<=hsum;j++){
if(i-1>=0 && n-i >=0)
ans=(ans+(dp[i-1][j]*dp[n-i][j])%m)%m;
}
ans=(ans*(k+1))%m;
ans=(ans-1+m)%m;
cout<<ans<<"\n";
}
//cout<<ans<<"\n";
}
return 0;
}
| #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define N 111
#define NN 401000
template <typename T> inline void read(T &x) {
x = 0; char c = getchar(); bool flag = false;
while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); }
while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); }
if (flag) x = -x;
}
using namespace std;
int n, K, P;
int ans[N], lmt[N];
int f[2][NN], g[2][NN], sum[NN];
inline void Try(int mid) {
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(sum, 0, sizeof(sum));
int up = min(lmt[mid - 1], lmt[n - mid]);
int nw = 0; f[nw][0] = 1;
for (int i = 1; i < mid; ++i) {
nw ^= 1;
int d = mid - i;
// ll del = 0;
for (int j = 0; j <= up; ++j) {
if (j - d >= 0) sum[j] = (sum[j - d] + f[nw ^ 1][j]) % P;
else sum[j] = f[nw ^ 1][j];
}
for (int j = 0; j <= up; ++j) {
if (j - (K+1) * d < 0) f[nw][j] = sum[j];
else f[nw][j] = (sum[j] - sum[j - (K+1) * d] + P) % P;
}
}
if (nw) memcpy(f[0], f[1], sizeof(f[1]));
nw = 0; g[nw][0] = 1;
for (int i = mid + 1; i <= n; ++i) {
nw ^= 1;
int d = i - mid;
for (int j = 0; j <= up; ++j) {
if (j - d >= 0) sum[j] = (sum[j - d] + g[nw ^ 1][j]) % P;
else sum[j] = g[nw ^ 1][j];
}
for (int j = 0; j <= up; ++j) {
if (j - (K+1) * d < 0) g[nw][j] = sum[j];
else g[nw][j] = (sum[j] - sum[j - (K+1) * d] + P) % P;
}
}
if (nw) memcpy(g[0], g[1], sizeof(g[1]));
for (int j = 0; j <= up; ++j) if (f[0][j] && g[0][j]) {
ans[mid] = (ans[mid] + 1ll * f[0][j] * g[0][j] % P * (K + 1)) % P;
}
}
int main() {
read(n), read(K), read(P);
for (int i = 1; i <= n; ++i) lmt[i] = lmt[i - 1] + K * i;
for (int i = 1; i <= (n + 1) >> 1; ++i) Try(i);
for (int i = 1; i <= n; ++i) if (!ans[i]) ans[i] = ans[n - i + 1];
for (int i= 1; i <= n; ++i) {
printf("%d\n", ((ans[i] - 1 + P) % P + P) % P);
}
return 0;
}
/*
3 1 998244353
//1 3 1
10 8 861271909
100 100 7
*/ |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a,b,c;
cin>>a>>b>>c;
ll x = max(a,max(b,c));
if(a == x) a = -1;
else if(b == x) b = -1;
else if(c == x) c = -1;
x += max(a,max(b,c));
cout<<x<<"\n";
}
| #include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define pb push_back
#define mod 1000000007
#define endl '\n'
const int N = 1e5 + 5; //change it when n7eeded
void solve() {
int a[3];
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
sort(a, a + 3);
cout << a[1] + a[2] << endl;
}
int32_t main() {
ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// int t; cin >> t; while (t--)
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long long arr[300010];
long long arr2[300010];
int main()
{
long long a,b,c,d,e,i,j;
cin >> a >> b;
long long k=0,chk=0;
for(i=0;i<a;i++)
{
cin >> d;
arr[d]++;
chk=max(d,chk);
k=max(arr[d],k);
}
for(i=0;i<=chk;i++){
if(i==0)
arr2[i]=min(arr[i],b);
else{
arr2[i]=min(arr[i],arr2[i-1]);
}
if(arr2[i]==0)
break;
}
long long past=0,sum=0;
for(i=chk;i>=0;i--){
long long present=arr2[i];
sum=sum+(present-past)*(i+1);
past=present;
}
cout << sum << endl;
}
| #include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<complex>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
#define removeduplicates(vec) vec.erase( unique( vec.begin(), vec.end() ), vec.end() )
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
void solve(){
ll n,k;
cin>>n>>k;
int a[n];
int mp[n+1]={0};
int maxi=0;
rep(i,n){
cin>>a[i];
mp[a[i]]++;
maxi=max(maxi,a[i]);
}
ll ans=0;
mp[maxi+1]=0;
for(ll x=0;x<=n;x++){
ll f=mp[x];
ll can=min(f,k);
ans+=(k-can)*x;
k=can;
}
cout<<ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin>>t;
while(t--){
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
if(s[0] == s[1] && s[0] == s[2]){
cout << "Won" << endl;
}else{
cout << "Lost" << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
set<char> var(begin(s),end(s));
if(var.size()==1){
cout<<"Won";}
else{
cout<<"Lost";}}
|
#include <bits/stdc++.h>
#ifdef DEBUG_BUILD
# include "debugger.hpp"
#else
# define debug(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using pll = std::pair<ll, ll>;
template <class T> using vec = std::vector<T>;
template <class T> using vec2 = std::vector<vec<T>>;
template <class T> using vec3 = std::vector<vec2<T>>;
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rrep(i, N) for (ll i = (N) - 1; i >= 0; i--)
#define range(i, A, B) for (ll i = (A); i < (B); i++)
constexpr int MOD = 1000000007;
constexpr ll INFL = std::numeric_limits<ll>::max();
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; }
ll pow10(ll n) {
ll r = 1;
rep(i, n) {
r *= 10;
}
return r;
}
ll score(const string& s, ll n) {
vec<ll> count(10);
rep(i, 4) {
count[s[i] - '0']++;
}
count[n]++;
ll point = 0;
rep(i, 10) {
point += i * pow10(count[i]);
}
return point;
}
void Main()
{
ll K;
string S, T;
cin >> K >> S >> T;
vec<ll> remc(10, K);
remc[0] = 0;
rep(i, 5) {
remc[S[i] - '0']--;
remc[T[i] - '0']--;
}
ll ret = 0;
for (ll send = 1; send <= 9; send++) {
if (remc[send] == 0)
continue;
debug(send);
const ll ss = remc[send];
debug(ss);
remc[send]--;
for (ll tend = 1; tend <= 9; tend++) {
if (remc[tend] == 0)
continue;
debug(tend);
const ll ts = remc[tend];
const ll sc = score(S, send);
const ll tc = score(T, tend);
debug(sc, tc);
if (sc > tc) {
ret += ss * ts;
}
}
remc[send]++;
debug(ret);
}
cout << (ld)ret / ((9*K-8)*(9*K-9)) << endl;
}
int main()
{
std::cout << std::fixed << std::setprecision(10);
Main();
std::cout << std::flush;
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
int main(){
long long k;
cin>>k;
long long count1[10]={0};
long long count2[10]={0};
long long count3[10]={0};
for(long long i=1;i<=9;i++){
count3[i]=k;
}
string s;
string t;
cin>>s;
cin>>t;
for(long long i=0;i<4;i++){
count1[s[i]-'0']++;
count3[s[i]-'0']--;
}
for(long long i=0;i<4;i++){
count2[t[i]-'0']++;
count3[t[i]-'0']--;
}
long long sum1=0;
long long sum2=0;
long long temp=(9*k-8)*(9*k-9);
long long sum3=0;
for(long long i=1;i<=9;i++){
sum1+=i*pow(10,count1[i]);
sum2+=i*pow(10,count2[i]);
}
for(long long i=1;i<=9;i++){
for(long long j=1;j<=9;j++){
if(i==j){
if((sum1-i*pow(10,count1[i])+i*pow(10,count1[i]+1))>(sum2-j*pow(10,count2[j])+j*pow(10,count2[j]+1))){
sum3+=(count3[i])*(count3[i]-1);
}
}
else{
if((sum1-i*pow(10,count1[i])+i*pow(10,count1[i]+1))>(sum2-j*pow(10,count2[j])+j*pow(10,count2[j]+1))){
sum3+=(count3[i])*(count3[j]);
}
}
}
}
double ans=(double)((double)sum3/temp);
printf("%.16f", ans);
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define LL long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)
constexpr int N = 1e3+9;
constexpr int M = 1e9+7;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif
int n;
char aa, ab, ba, bb;
int fact[N], inv[N];
int qpow(int a, int b){
int c = 1;
while(b){
if(b & 1) c = c * a % M;
a = a * a % M;
b >>= 1;
}
return c;
}
int C(int n,int m){
if(n < 0 || m > n) return 0;
return fact[n] * inv[m] % M * inv[n-m] % M;
}
void init(){
fact[0] = 1;
for (int i = 1; i < N; i++) fact[i] = fact[i-1] * i % M;
inv[N-1] = qpow(fact[N-1], M - 2);
for(int i= N-2; i >= 0; i--) inv[i] = inv[i+1] * (i+1) % M;
}
void add(int& x, int y) {
x %= M, y %= M;
x = (x + M + y) % M;
}
int Lucas(int n, int m) {
if(n < 0 || m > n) return 0;
if (m == 0) return 1;
return (C(n % M, m % M) * Lucas(n / M, m / M)) % M;
}
// 想清楚计数的依据
signed main() {
fastio;
init();
cin >> n >> aa >> ab >> ba >> bb;
if (n <= 3) {
cout << 1 << "\n";
return 0;
}
if (ab == 'A') {
if (aa == 'A') {
cout << 1 << "\n";
} else {
if (ba == 'A') {
int ans = 0;
for (int innerB = 0; innerB*2+2 <= n; innerB++) {
add(ans, C(n-1-innerB-1, innerB));
}
cout << ans << "\n";
} else {
cout << qpow(2, n-3) << "\n";
}
}
} else {
if (bb == 'B') {
cout << 1 << "\n";
} else {
if (ba == 'B') {
int ans = 0;
for (int innerB = 0; innerB*2+2 <= n; innerB++) {
add(ans, C(n-1-innerB-1, innerB));
}
cout << ans << "\n";
} else {
cout << qpow(2, n-3) << "\n";
}
}
}
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin >> a>>b>>c>>d;
cout << max(a,b)-min(c,d);
return 0;
} |
#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
string p[520];
signed main(){
int mx = 0;
int h,w;
cin >> h >> w;
int ans = 1;
for(int i = 0;i < h;i++){
cin >> p[i];
}
bool able = true;
for(int i = 0;i < w;i++){
int sti = i;
int stj = 0;
bool c = true;
int cnt = 0;
map<char,int> mp;
while(sti >= 0 && stj < h){
if(p[stj][sti] != '.'){
if(mp[p[stj][sti]] == 0){
cnt += 1;
mp[p[stj][sti]] = 1;
if(cnt == 2)able = false;
}
c = false;
}
sti--;
stj++;
}
if(c) ans = (ans * 2ll)% 998244353;
}
for(int i = 1;i < h;i++){
int sti = w - 1;
int stj = i;
bool c = true;
int cnt = 0;
map<char,int> mp;
while(sti >= 0 && stj < h){
if(p[stj][sti] != '.'){
if(mp[p[stj][sti]] == 0){
cnt += 1;
mp[p[stj][sti]] = 1;
if(cnt == 2)able = false;
}
c = false;
}
sti--;
stj++;
}
if(c) ans = (ans * 2ll)% 998244353;
}
if(able == true)cout << ans << endl;
else cout << 0 << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define For(i,a,b) for(reg int i=a;i<=b;++i)
#define Down(i,a,b) for(reg int i=a;i>=b;--i)
#define ull unsigned long long
#define ll long long
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int gcd(int n,int m){return m?gcd(m,n%m):n;}
inline int lcm(int x,int y){return x/gcd(x,y)*y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
namespace yspm{
inline int read(){
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
const int N=510;
char s[N<<1][N<<1];
int n,m;
signed main(){
n=read(); m=read(); For(i,1,n) scanf("%s",s[i]+1);
for(reg int i=1;i<=m+n;++i) for(reg int j=1;j<=m+n;++j) if(s[i][j]==0) s[i][j]='.';
int ans=1;
if(s[n][m]=='.') ans*=2;
for(reg int S=2;S<n+m;++S){
bool ad=1; char dif='.';
for(reg int j=1;j<S;++j){
if(s[S-j][j]!='.'){
ad=0;
if(dif=='.') dif=s[S-j][j];
else if(s[S-j][j]!=dif) puts("0"),exit(0);
}
} if(ad) ans=ans*2%998244353;
} cout<<ans<<endl;
return 0;
}
}signed main(){return yspm::main();}
//Use The Time To Enrich This Selfclosing Youth
|
#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 int long long
#define ull unsigned long long
#define ll long long
#define M 998244353
#define pb push_back
#define p_q priority_queue
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<pii,ll>
#define all(a) (a).begin(),(a).end()
#define sz(x) (ll)x.size()
#define endl '\n'
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b)) / gcd((a),(b))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rep(i, begin, end) for (int i=begin;i<end;i++)
#define repd(i, begin, end) for (int i=end-1;i>=begin;i--)
#define ini(a,n,b) for(ll int i=0;i<n;i++) a[i]=0;
#define cset(a) __builtin_popcountll(a)
#define hell 1000000007
#define re resize
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update >
const int INF=1e18;
const int N=3e5+5;
int powm(int a,int b,int mod)
{
int res=1;
while(b)
{
if(b&1)
res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
int pow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=(res*a);
a=(a*a);
b>>=1;
}
return res;
}
vi par(N,1),siz(N,1);
void init(int n)
{
for(int i=1;i<=n;i++) par[i]=i;
}
int find(int x)
{
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
if(x==y) return;
if(siz[x]<siz[y]) swap(x,y);
par[y]=x;
siz[x]+=siz[y];
}
signed main(void)
{ios
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout);
#endif
int tests=1;
// cin>>tests;
while(tests--)
{
int n;
cin>>n;
vi a(n+1);
rep(i,1,n+1) cin>>a[i];
int ans=0;
vi vis(n+1,0);
init(n);
rep(i,1,n+1)
{
merge(i,a[i]);
}
rep(i,1,n+1)
{
if(par[i]==i) ans++;
}
ans=powm(2,ans,M);
ans=(ans-1+M)%M;
cout<<ans<<endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T>
struct BIT {
int n;
vector<T> dat;
BIT(int n) : n(n), dat(n + 1, 0) {}
T sum(int idx) { // 1-indexed
T res(0);
for (int i = idx; i > 0; i -= i & -i) res += dat[i];
return res;
}
T sum(int l, int r) { // 0-indexed
return sum(r) - sum(l);
}
void add(int idx, T x) { // 0-indexed
idx++;
for (int i = idx; i <= n; i += i & -i) dat[i] += x;
}
int lower_bound(T x) {
if (x <= 0) return T(0);
int res = 0, r = 1;
while (r < n) r <<= 1;
for (; r > 0; r >>= 1) {
if (res + r <= n && dat[res + r] < x) {
x -= dat[res + r];
res += r;
}
}
return res + 1;
}
void print() {
for (int i = 0; i < n; i++) cout << sum(i, i + 1) << " ";
cout << endl;
}
};
signed main() {
int h, w, m;
cin >> h >> w >> m;
int mnx[w], mny[h];
fill(mnx, mnx + w, h);
fill(mny, mny + h, w);
vector<int> vy[h];
int x, y;
rep(i, m) {
cin >> x >> y;
x--;
y--;
chmin(mnx[y], x);
chmin(mny[x], y);
vy[x].emplace_back(y);
}
ll ans = 0;
rep(y, w) {
if (mnx[y] == 0) break;
ans += mnx[y];
}
BIT<int> bit(w);
rep(x, h) {
if (mny[x] == 0) break;
ans += bit.sum(0, mny[x]);
if (x == 0) {
for (int y = mny[x]; y < w; y++) {
bit.add(y, 1);
}
continue;
}
for (int y : vy[x]) {
if (bit.sum(y, y + 1) == 0) bit.add(y, 1);
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++)
#define drep(i, n, l) for (int i = (int)(n); i > (int)(l); i--)
#define INF INT_MAX
#define INFF (pow(2,64))
#define def (200007)
#define MOD (1000000007)
typedef int64_t intt;
typedef vector<int> veci;
typedef vector<vector<int>> Veci;
typedef vector<int64_t> vecit;
typedef vector<vector<int64_t>> Vecit;
typedef vector<vector<double>> Vecd;
typedef vector<double> vecd;
typedef pair<intt, intt> P;
typedef unordered_set<int> uset;
typedef unordered_set<intt> usett;
vector<vector<vector<double>>>dp;
int A,B,C;
double dfs(int a,int b,int c){
if(dp[a][b][c]!=-1)return dp[a][b][c];
if(a==100 || b==100 || c==100)return 0;
double sum=0;
sum+=(dfs(a+1,b,c)+1)*a/(double)(a+b+c)+(dfs(a,b+1,c)+1)*b/(double)(a+b+c)+(dfs(a,b,c+1)+1)*c/(double)(a+b+c);
dp[a][b][c]=sum;
return sum;
}
int main(){
cin>>A>>B>>C;
dp.resize(101);
rep(i,0,101){
dp[i].resize(101);
}
rep(i,0,101){
rep(j,0,101){
dp[i][j].assign(101,-1);
}
}
dfs(A,B,C);
cout<<fixed<<setprecision(10);
cout<<dp[A][B][C]<<endl;
} | #pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
for (auto e : v) {
o << e << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
#if 1
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
vi cs(3);
cin >> cs;
vector<vector<vector<ld>>> ca(100, vv(ld)(100, v(ld)(100,-1)));
auto dfs = [&](auto dfs, vi& cs, int sum)->ld {
if (cs[0] >= 100||cs[1]>=100||cs[2]>=100) {
return 0;
}
if (ca[cs[0]][cs[1]][cs[2]] >= 0) {
return ca[cs[0]][cs[1]][cs[2]];
}
ld re = 0;
F0R(i, 3) {
if (cs[i]) {
cs[i]++;
re += (dfs(dfs, cs, sum + 1) + 1) * (cs[i]-1) / sum;
cs[i]--;
}
}
return ca[cs[0]][cs[1]][cs[2]] = re;
};
cout << std::fixed << std::setprecision(15);
cout << dfs(dfs, cs, cs[0]+cs[1]+cs[2]);
}
#endif
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include<bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class CFairElevator {
public:
void solve(istream& cin, ostream& cout) {
SPEED;
int n; cin >> n;
vector<int> a(n), b(n);
rep(i, n) {
cin >> a[i] >> b[i];
}
vector<bool> ok(n+1);
ok[0] = true;
for(int k=0; k<n; k++) {
if (!ok[k]) continue;
for(int j=k+1; j<=n; j++) {
int left = k*2;
int right = j*2;
int diff = j-k;
// (left , right]
vector<int> us(2*n+1);
bool flag = true;
rep(i, n) {
if (a[i] != -1) {
if (us[a[i]]) {
flag = false;
break;
} else {
us[a[i]] = true;
}
}
if (b[i] != -1) {
if (us[b[i]]) {
flag = false;
break;
} else {
us[b[i]] = true;
}
}
if (a[i] == -1 && b[i] == -1) continue;
if (a[i] == -1 && b[i] > left && b[i] <= right) {
int pos = b[i] - diff;
if (pos <= left || us[pos]) {
flag = false;
break;
} else {
us[pos] = true;
}
}
if (b[i] == -1 && a[i] > left && a[i] <= right) {
int pos = a[i] + diff;
if (pos > right || us[pos]) {
flag = false;
break;
} else {
us[pos] = true;
}
}
if (a[i] == -1 || b[i] == -1) continue;
if (b[i] <= left) continue;
if (a[i] > right) continue;
if (a[i] > left && b[i] <= right) {
if (b[i] - a[i] != diff) {
flag = false;
break;
}
} else {
flag = false;
break;
}
}
if (flag) {
ok[j] = true;
if (j==n) {
cout << "Yes" << endl;
return;
}
}
}
}
cout << "No" << endl;
}
};
signed main() {
CFairElevator solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
template<class T, class U> using P = pair<T,U>;
template<class T> using Heap = priority_queue<T>;
template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>;
template<class T,class U> using umap = unordered_map<T,U>;
template<class T> using uset = unordered_set<T>;
template<class T>
bool ChangeMax(T&a,const T&b){
if(a >= b) return false;
a = b; return true;
}
template<class T>
bool ChangeMin(T&a,const T&b){
if(a <= b) return false;
a = b; return true;
}
template<class T, size_t N, class U>
void Fill(T (&a)[N], const U&v){
fill((U*)a,(U*)(a+N),v);
}
template<class T>
istream& operator >> (istream&is, vector<T>&v){
for(auto&e:v)is >> e;
return is;
}
int n;
vector<int> A,B,in,out;
map<int,int> mp;
bool Recc(int p,int l){
static bool mm[201][101];
static bool vs[201][101];
if(vs[p][l])return mm[p][l];
vs[p][l] = 1;
bool f = true;
for(int i = 0; i < l && f; ++i){
if(p+i+l >= 2*n){f = false;break;}
f &= (in[p+i]<0 || out[p+i+l]<0) || in[p+i] == out[p+i+l];
f &= out[p+i]<0 && in[p+i+l]<0;
if((mp.count(in[p+i])&&mp[in[p+i]] != l) || (mp.count(out[p+i+l])&&mp[out[p+i+l]] != l))f = false;
}
return mm[p][l] = f;
}
bool Rec(int p){
if(p >= 2*n)return true;
static bool rmm[201];
static bool rvs[201];
if(rvs[p])return rmm[p];
rvs[p] = 1;
bool f = false;
for(int i = 1; i < 101; ++i){
bool g = Recc(p,i);
if(!g)continue;
f = Rec(p+i*2);
if(f)break;
}
return rmm[p] = f;
}
int main(){
cin >> n;
B = A = vector<int>(n);
in = out = vector<int>(n+n,-1);
bool fff = true;
bitset<1000> cnt;
for(int i = 0; i < n; ++i){
cin >> A[i] >> B[i];A[i]--;B[i]--;
if(A[i]>=0){
if(cnt[A[i]])fff = false;
cnt[A[i]] = 1;
}
if(B[i]>=0){
if(cnt[B[i]])fff = false;
cnt[B[i]] = 1;
}
if(B[i]>=0&&A[i]>=0&&A[i] >= B[i])fff = false;
if(A[i] >= 0)in[A[i]] = i;
if(B[i] >= 0)out[B[i]] = i;
if(A[i] >= 0 && B[i] >= 0)mp[i] = B[i] - A[i];
}
// for(int i = 0; i < n+n; ++i){
// debug(in[i]);
// debug(out[i]);
// }
cout << (fff&&Rec(0) ? "Yes" : "No") << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000;//10の12乗
int main() {
ll h,w,n,m;
cin >> h >> w >> n >> m;
VVl aka(h,Vl(w)),lit(h,Vl(w));
rep(i,n){
ll x,y;
cin >> x >> y;
x--;y--;
aka[x][y]+=2;
}
rep(i,m){
ll x,y;
cin >> x >> y;
x--;y--;
aka[x][y]--;
}
rep(i,h)rep(j,w){
if(aka[i][j]>0){
while(j>0){
if(aka[i][j-1]==-1)break;
j--;
}
while(j<w){
if(aka[i][j]==-1){
//j++;
break;
}
lit[i][j]++;
j++;
}
}
//cout << j << endl;
}
rep(j,w)rep(i,h){
if(aka[i][j]>0){
while(i>0){
if(aka[i-1][j]==-1)break;
i--;
}
while(i<h){
if(aka[i][j]==-1){
//i++;
break;
}
lit[i][j]++;
i++;
}
//cout << i << endl;
}
}
ll ans=0;
rep(i,h)rep(j,w){
if(lit[i][j]>=1&&aka[i][j]>=0) ans++;
//cout << lit[i][j] << endl;
}
cout << ans << endl;
} | //Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vpi;
#define pb push_back
#define pf push_front
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define Tcase() int tcc; cin >> tcc; while(tcc--)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
const ll MOD = 998244353;
const ll INF = 9e18;
const int MX = 2e5 + 5;
const ld PI = acos((ld) -1);
const int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
void setIO(string name = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
if(sz(name)){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
int getXOR(int BITree[], int index){
int ans = 0;
index += 1;
while(index > 0){
ans ^= BITree[index];
index -= index & (-index);
}
return ans;
}
void updateBIT(int BITree[], int n, int index, int val){
index = index + 1;
while (index <= n){
BITree[index] ^= val;
index += index & (-index);
}
}
int* constructBITree(int arr[], int n){
int* BITree = new int[n + 1];
for (int i = 1; i <= n; i++)
BITree[i] = 0;
for (int i = 0; i < n; i++)
updateBIT(BITree, n, i, arr[i]);
return BITree;
}
int rangeXor(int BITree[], int l, int r){
return getXOR(BITree, r) ^ getXOR(BITree, l - 1);
}
int main(){
int n, Q;
cin >> n >> Q;
int A[n + 1];
FOR(i, 0, n) cin >> A[i];
int q[Q][3];
F0R(i, Q) cin >> q[i][0] >> q[i][1] >> q[i][2];
int* BITree = constructBITree(A, n);
for(int i = 0; i < Q; i++){
int id = q[i][0];
if(id == 2){
int L = q[i][1];
int R = q[i][2];
cout << rangeXor(BITree, L - 1, R - 1) << "\n";
}
else{
int idx = q[i][1];
int val = q[i][2];
A[idx - 1] ^= val;
updateBIT(BITree, n, idx - 1, val);
}
}
}
|
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define ll long long
#define pii std::pair<int, int>
#define pdd std::pair<double, double>
#define INF (1LL << 33)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(p) \
if (test) \
cout << #p " : " << p << endl;
bool test = false;
template <class T>
[[maybe_unused]] bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
};
int N, M;
vector<int> A, B;
vector<vector<int>> dp;
signed main()
{
test = true;
cin >> N >> M;
A.resize(N);
B.resize(M);
for (auto& v : A) {
cin >> v;
}
for (auto& v : B) {
cin >> v;
}
dp.resize(N + 1);
REP(i, N + 1)
{
dp.at(i).resize(M + 1, 0);
REP(j, M + 1)
{
if (i == 0) {
dp.at(i).at(j) = j;
} else if (j == 0) {
dp.at(i).at(j) = i;
} else {
dp.at(i).at(j) = dp.at(i - 1).at(j) + 1;
chmin(dp.at(i).at(j), dp.at(i).at(j - 1) + 1);
chmin(dp.at(i).at(j), dp.at(i - 1).at(j - 1) + (A.at(i - 1) == B.at(j - 1) ? 0 : 1));
}
}
}
cout << dp.at(N).at(M) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define SIZE 200005
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
int A[SIZE];
int L[SIZE],R[SIZE];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
A[i]--;
}
set <int> zan,need;
for(int i=0;i+1<n;i++)
{
if(need.find(A[i])!=need.end()) need.erase(A[i]);
else zan.insert(A[i]);
if(zan.find(i)!=zan.end()) zan.erase(i);
else need.insert(i);
if(zan.size()!=1)
{
puts("-1");
return 0;
}
L[i]=*zan.begin();
R[i]=*need.begin();
//printf("%d %d\n",L[i],R[i]);
}
queue <int> que;
for(int i=0;i<n-1;i++)
{
if(A[i]==L[i]&&A[i+1]==R[i])
{
que.push(i);
}
}
vector <int> vec;
while(!que.empty())
{
int v=que.front();que.pop();
vec.push_back(v);
swap(A[v],A[v+1]);
if(v>0&&L[v-1]==A[v-1]&&R[v-1]==A[v]) que.push(v-1);
if(v<n-1&&L[v+1]==A[v+1]&&R[v+1]==A[v+2]) que.push(v+1);
}
if(vec.size()<n-1) puts("-1");
else
{
for(int i=0;i<n-1;i++) printf("%d\n",vec[i]+1);
}
} |
#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, x;
read(n, x);
auto a = read_array<int>(n);
for(auto y : a){
if(y != x) write(y, " ");
}
write("\n");
}
int main()
{
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
solve();
return 0;
}
| #include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep3(i, a, b) for (ll i = a; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
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;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
template <class T> void takeUnique(vector<T> &v) {
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());
}
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1};
// clang++ -std=c++11 -stdlib=libc++
int n, x;
int main() {
cin >> n >> x;
vi ans;
rep(i, n) {
int a;
cin >> a;
if (a == x)
continue;
ans.pb(a);
}
rep(i, ans.size()) {
printf("%d%c\n", ans[i], i == ans.size() - 1 ? '\n' : ' ');
}
return 0;
}
|
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N; cin >> N;
VI P(N), pos(N);
REP(i, N) {
cin >> P[i];
pos[P[i] - 1] = i;
}
VI A = P;
SORT(A);
VI ans;
int now = 0;
vector<bool>F(N, false);
REP(i, N - 1) {
//REP(i, N)cout << pos[i] << endl;
//cout << endl;
while (pos[now] <= now) {
now++;
if (now == N) {
cout << -1 << endl;
return 0;
}
}
{
if (!F[pos[now]]) {
F[pos[now]] = 1;
ans.push_back(pos[now]);
}
else {
cout << -1 << endl;
return 0;
}
pos[P[pos[now] - 1] - 1]++;
pos[now]--;
}
}
fore(i, ans) {
swap(P[i - 1], P[i]);
}
if (A == P)fore(i, ans)cout << i << '\n';
else cout << -1 << endl;
return 0;
}
| /*include&using-----------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;
/*debug-------------------------------------------------------------------------------*/
#define _GLIBCXX_DEBUG
#ifdef _DEBUG
#define debug(...) COUT(__VA_ARGS__)
#else
#define debug(...)
#endif
/*typename----------------------------------------------------------------------------*/
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using P = pair<T,T>;
template <class T> using M = map<T,T>;
template <class T> using S = set<T>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T,V<T>,greater<T>>;
using ll = long long;
using st = string;
using vl = V<ll>;
using vvl = V<V<ll>>;
using vc = V<char>;
using vvc = V<V<char>>;
using vs = V<st>;
using vvs = V<V<st>>;
using vb = V<bool>;
using vvb = V<V<bool>>;
/*constant----------------------------------------------------------------------------*/
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
/*macro-------------------------------------------------------------------------------*/
#define re(n) for(ll _ = 0; _ < (ll) n; _++)
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--)
#define repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++)
#define each(x,A) for(auto &(x): (A))
#define all(A) A.begin(), A.end()
#define len(A) ll(A.size())
#define pb(a) push_back(a)
#define mp make_pair
#define pc cout << setprecision(15) << fixed
/*input-------------------------------------------------------------------------------*/
void CIN(){}
template <class Head,class... Tail>
void CIN(Head&& h,Tail&&... t){cin>>h; CIN(t...);}
#define CINL(...) ll __VA_ARGS__;CIN(__VA_ARGS__)
#define CINS(...) string __VA_ARGS__;CIN(__VA_ARGS__)
#define CIND(...) double __VA_ARGS__;CIN(__VA_ARGS__)
/*output------------------------------------------------------------------------------*/
void COUT(){cout << endl;}
template <class Head, class... Tail>
void COUT(Head h, Tail... t){cout << h << " "; COUT(t...);}
/*function----------------------------------------------------------------------------*/
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
template <class T> T ceil(T a,T b){return (a+b-1)/b;}
template <class T> void chmax(T &a, const T& b){if(a<b){a=b;}}
template <class T> void chmin(T &a, const T& b){if(a>b){a=b;}}
template <class T> T gcd(T a,T b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
template <class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template <class T> T modpow(T x,T n,T mod){T res=1; for(ll i=0;i<n;i++){res=res*x%mod;}return res;}
bool range(ll ny, ll nx, ll H, ll W) {if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;}
/*------------------------------------------------------------------------------------*/
int main() {
CINS(s);
rep(i,len(s)){
if(i%2==0 && !('a'<=s[i] && s[i]<='z')){
COUT("No");
return 0;
}
if(i%2==1 && !('A'<=s[i] && s[i]<='Z')){
COUT("No");
return 0;
}
}
COUT("Yes");
} |
#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 ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int32_t main()
{
int n,q;
cin>>n>>q;
ordered_set input;
for(int i=0; i<n; i++)
{
int temp;
cin>>temp;
input.insert(temp);
}
while(q--)
{
int k;
cin>>k;
int ans=k;
int less = input.order_of_key(ans+1);
/*
3 5 6 7
k==5
less = 2
k==7
less = 2
k==9
less = 0
*/
int prev = less;
while(less!=0)
{
ans+=less;
less = input.order_of_key(ans+1);
int temp=prev;
prev=less;
less-=temp;
}
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
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);
cout.tie(NULL);
long long int n;
int m;
cin >> n >> m;
vector <long long int> v;
long long int a[m];
for (int i = 0; i < m; i++) cin >> a[i];
sort(a, a + m);
if (m == 0) {
cout << 1 << "\n";
}
else {
for (int i = 0; i < m; i++) {
if (i == 0) {
long long int tmp = a[i] - 1;
if (tmp) v.push_back(tmp);
}
else {
long long int tmp = a[i] - a[i - 1] - 1;
if (tmp) v.push_back(tmp);
}
}
if (a[m - 1] != n) {
v.push_back(n - a[m - 1]);
}
sort(v.begin(), v.end());
if (v.size()) {
long long int ans = 0;
long long int tmp = v[0];
for (auto i : v) {
ans += (i + tmp - 1) / tmp;
}
cout << ans << "\n";
}
else cout << 0 << "\n";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct point {
T x, y;
point() : x(), y() { }
point(T x, T y) : x(x), y(y) { }
point operator+() const { return *this; }
point operator-() const { return point(-x, -y); }
point &operator+=(const point &p) { return x += p.x, y += p.y, *this; }
point &operator-=(const point &p) { return x -= p.x, y -= p.y, *this; }
point &operator*=(const T &val) { return x *= val, y *= val, *this; }
point &operator/=(const T &val) { return x /= val, y /= val, *this; }
point operator+(const point &p) { return point(x + p.x, y + p.y); }
point operator-(const point &p) { return point(x - p.x, y - p.y); }
point operator*(const T &val) { return point(x * val, y * val); }
point operator/(const T &val) { return point(x / val, y / val); }
friend T dot(const point &a, const point &b) {
return a.x * b.x + a.y * b.y;
}
friend point operator*(const T &val, const point &p) { return point(val * p.x, val * p.y); }
bool operator==(const point &p) const {
return x == p.x && y == p.y;
}
friend ostream &operator<<(ostream &os, const point &p) {
return os << '(' << p.x << ',' << p.y << ')';
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
int m;
cin >> m;
vector<point<long long>> pref_x(m + 1), pref_y(m + 1);
vector<int> swaps(m + 1);
pref_x[0] = pref_y[0] = {0, 1};
for (int i = 1; i <= m; i++) {
int t, p;
cin >> t;
pref_x[i] = pref_x[i - 1], pref_y[i] = pref_y[i - 1], swaps[i] = swaps[i - 1];
if (t == 1) {
swaps[i] ^= 1;
swap(pref_x[i], pref_y[i]);
pref_y[i] = -pref_y[i];
} else if (t == 2) {
swaps[i] ^= 1;
swap(pref_x[i], pref_y[i]);
pref_x[i] = -pref_x[i];
} else if (t == 3) {
cin >> p;
pref_x[i] = point<long long>(2 * p, 0) - pref_x[i];
} else {
cin >> p;
pref_y[i] = point<long long>(2 * p, 0) - pref_y[i];
}
}
//1 = (y, -x)
//2 = (-y, x)
//3, (2p - x, y)
//4, (x, 2p - y)
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b, --b;
if (!swaps[a]) {
cout << pref_x[a].x + pref_x[a].y * x[b] << ' ';
cout << pref_y[a].x + pref_y[a].y * y[b] << '\n';
} else {
cout << pref_x[a].x + pref_x[a].y * y[b] << ' ';
cout << pref_y[a].x + pref_y[a].y * x[b] << '\n';
}
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
const int N = 1e5 + 1;
int arr[N], n;
double fun(double x){
double ans =0 ;
for(int i =0 ; i < n; i++){
ans += (x + (double)(arr[i]) - min((double)(arr[i]), 2.0*x));
}
return ans;
}
void solve()
{
cin>> n;
int mn = 1e9;
for(int i =0 ; i < n;i ++){
cin>> arr[i];
mn = min(arr[i], mn);
}
double l = (double(mn) / 2.0), r = 1e9, e = 1e-7;
while(r - l >= e){
double m1 = l + (r - l) / 3;
double m2 = r - (r - l) / 3;
double v1 = fun(m1);
double v2 = fun(m2);
if(v1 > v2){
l = m1;
}else{
r = m2;
}
}
double x = r;
double ans = fun(x) / (double(n));
cout<< ans << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// freopen("Debug.txt", "w", stderr);
#else
#endif
cout << fixed << setprecision(10);
int _t =1;
// cin>>_t;
F(int, i, 1, _t, 1){
// cout << "Case #" << i << ": ";
solve();
}
}
|
//https://atcoder.jp/contests/abc183/tasks/abc183_c
//C - Travel
#include <bits/stdc++.h>
using namespace std;
const int MAXN=10;
int nums[MAXN][MAXN];
int main() {
int n,k;
cin>>n>>k;
int i;
for (i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
cin>>nums[i][j];
}
}
vector<int> idx(n+2);
iota(idx.begin(), idx.end(), 0);
idx[n+1]=1;
int ans=0;
do {
int sum=0;
for (i=2; i<=n+1; i++) {
sum+=nums[idx[i-1]][idx[i]];
}
if (sum==k) {
ans++;
}
} while (next_permutation(idx.begin()+2, idx.end()-1));
cout<<ans<<"\n";
return 0;
} | #include <bits/stdc++.h>
#include <math.h>
#include <cmath>
using namespace std;
using ll =long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
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; }
const long long INF = 1LL << 60;
typedef pair<int, int> PR;
#define rep(i,n) ;for( int i =0; i < n ; i ++)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define fr(i,a,b) for(int i=a;i<b;i++)
#define pri(a) printf("%.14lf\n",a);
#define MOD 1000000007
bool is_int_lround(double x){
return std::lround(x)==x;
}
ll keta(ll x){
ll n=0;
while(x>0){
x /=10 ;
n ++;
}
return n;
}
ll conbi(int n,int m){
cin>>n>>m;
vector<ll> a(100);
a[0] =1;
for(int i=0;i<14;i++){
a[i+1]=a[i]*(i+1);
}
return a[n] /(a[m] *a[n-m]);
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;//繰り返し二乗法
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll kaijo(ll x){
ll z=1;
if(x==0){
return 1;
}
while(x>0){
z *=x;
z %=MOD;
x--;
}
return z;
}
ll yakusu_num(ll n){
vl yakusu(n+1,1);
for(ll i=2;i*i<=n;i++){
while(n%i==0){
n /=i;
yakusu[i]++;
}
}
if(n!=1)yakusu[n]++;
ll num=1;
for(ll i=0;i <=n;i++){
num*=yakusu[i];
}
return num;
}
//cout<<""<<endl;
int main(){
ll N,M;cin>>N>>M;
vl H(N),W(M);
rep(i,N)cin>>H[i];
rep(i,M)cin>>W[i];
sort(all(H));
sort(all(W));
vl wa1(N);
vl wa2(N);
wa1[0]=-H[0];
wa2[0]=H[0];
for(ll i=1;i<N;i++){
if(i%2==1){
wa1[i]=wa1[i-1]+H[i];
wa2[i]=wa2[i-1]-H[i];
}
else{
wa1[i]=wa1[i-1]-H[i];
wa2[i]=wa2[i-1]+H[i];
}
}
ll ans=1000000000000000;
rep(i,M){
ll cnt=0;
auto iter = lower_bound(all(H), W[i]);
ll x= iter-H.begin();
if(x%2==1){
cnt+=W[i];
cnt +=wa1[x-1]+wa2[N-1]-wa2[x-1];
}
else{
cnt-=W[i];
cnt +=wa1[x-1]+wa2[N-1]-wa2[x-1];
}
ans =min(ans,cnt);
}
cout<<ans<<endl;
}
|
/*問題番号:A*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
if(N%100 == 0){
cout << N/100 << endl;
}
else{
cout << N/100 + 1 << endl;
}
} |
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
void Main() {
// input
ll N;
cin >> N;
// process
// output
cout << N - 1 << endl;
}
int main() {
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
|
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define ll long long
#define for1(i,n,a,b) for(ll (i)=(n);(i)<=(a);(i)+=(b))
#define for2(i,n,a,b) for(ll (i)=(n);(i)>=(a);(i)-=(b))
ll a[1005];
int main()
{
ll n;
cin>>n;
for1(i,1,n,1)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for1(i,2,n,1)
{
if(a[i]-a[i-1]!=1)
{
puts("No");
return 0;
}
}
puts("Yes");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
// mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rng(int lim)
// {
// uniform_int_distribution<int> uid(0, lim - 1);
// return uid(rang);
// }
const int mod = 1'000'000'007;
//=======================================================================
int isPalindrome(string str)
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = str.size() - 1;
// Keep comparing characters while they are same
while (h > l)
{
if (str[l++] != str[h--])
{
//printf("%s is Not Palindrome", str);
return 0;
}
}
return 1;
}
int mpow(int base, int exp)
{
base %= mod;
int result = 1;
while (exp > 0)
{
if (exp & 1)
result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
int getSum(int n)
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
int countDigit(int n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
ll factorial(ll n)
{
ll res = 1, i;
for (i = 2; i <= n; i++)
res *= ((i * 1LL) % mod);
return res;
}
//----------------------------------------------------------------------------
int main()
{
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// srand(chrono::high_resolution_clock::now().time_since_epoch().count());
ll i, j, n, a[100000], count = 0, sum = 0;
unordered_set<int> s;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> j;
s.insert(j);
}
if (s.size() == n)
{
cout << "Yes";
}
else
{
cout << "No";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ll long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rfo(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define fo(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define see(x) cout << (x) << endl
#define hh cout << endl
#define INF 1e18
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
template <typename C,
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename std::enable_if<!std::is_same<C, std::string>::value>::type * = nullptr>
std::ostream &operator<<(std::ostream &os, const C &container)
{
bool first = true;
std::stringstream ss;
ss << '[';
for (const auto &x : container)
{
if (!first)
{
ss << ", ";
}
first = false;
ss << x;
}
ss << ']';
return os << ss.str();
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{
os << '{' << p.first << ", " << p.second << '}';
return os;
}
ll mod_pow(ll a, ll b)
{
if (b < 0)
return 0;
a %= mod;
if (b == 0)
return 1;
if (b == 1)
return a % mod;
if (b == 2)
return ((a % mod) * (a % mod)) % mod;
return (mod_pow(mod_pow(a, b / 2), 2) * mod_pow(a, b % 2)) % mod;
}
bool isPrime(int n)
{
if (n == 1)
return false;
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Keep calm and Keep coding //
void solve()
{
int n, m;
cin >> n;
int a = ceil((sqrt(1 + 8 * n) - 1) / 2.0);
cout << a;
hh;
return;
}
int32_t main()
{
fast int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const ll m = 998244353;
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
ll a,b,c; cin >> a >> b >> c;
ll ans = 1;
a = a*(a+1)/2;
b = b*(b+1)/2;
c = c*(c+1)/2;
a %= m;
b %= m;
c %= m;
ans = (ans*a)%m;
ans = (ans*b)%m;
ans = (ans*c)%m;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;
vector<int> v = {1, 1, 2, 6, 24};
int ans = 0;
vector<int> v1;
int n;
void fun(int x, int c, vector<int> a)
{
if(c == 4)
{
int t1 = 24;
for(int i = 0; i < 10; i++)
t1 /= v[a[i]];
ans += t1;
return;
}
if(x >= n | c > 4)
return;
fun(x+1, c, a);
a[v1[x]]++;
fun(x+1, c+1, a);
a[v1[x]]++;
fun(x+1, c+2, a);
a[v1[x]]++;
fun(x+1, c+3, a);
a[v1[x]]++;
fun(x+1, c+4, a);
a[v1[x]] -= 4;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(20) << fixed;
string s;
cin >> s;
int c = 0;
int i = 0;
vector<int> a(10, 0);
for(char x : s)
{
if(x == 'o')
{
c++;
a[i]++;
}
if(x == 'o' || x == '?')
v1.push_back(i);
i++;
}
n = v1.size();
fun(0, c, a);
cout << ans << endl;
return 0;
}
| /*
Author : $%U%$
Time : $%h%$:$%m%$:$%s%$, $%D%$/$%M%$/$%Y%$
*/
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
// bitwise ops
// also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until USACO updates ...
return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x))
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; }
tcTU> T fstTrue(T lo, T hi, U f) {
hi ++; assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo+(hi-lo)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
lo --; assert(lo <= hi); // assuming f is decreasing
while (lo < hi) { // find first index such that f is true
T mid = lo+(hi-lo+1)/2;
f(mid) ? lo = mid : hi = mid-1;
}
return lo;
}
tcT> void remDup(vector<T>& v) { // sort and remove duplicates
sort(all(v)); v.erase(unique(all(v)),end(v)); }
tcTU> void erase(T& t, const U& u) { // don't erase
auto it = t.find(u); assert(it != end(t));
t.erase(it); } // element that doesn't exist from (multi)set
#define el '\n'
void solve()
{
str st; cin>>st;
int ans = 0;
for(int i=0;i<=9999;i++)
{
bool arr[10];
memset(arr,false,sizeof(arr));
int temp = i;
for(int j=0;j<4;j++)
{
arr[temp%10] = true;
temp /= 10;
}
bool flag = true;
for(int j=0;j<10;j++)
{
if(st[j]=='o' and !arr[j]) flag = false;
if(st[j]=='x' and arr[j]) flag = false;
}
ans += flag;
}
cout<<ans;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int ct=1;
// cin>>ct;
while(ct--)
{
solve();
}
} |
#include <bits/stdc++.h>
#define rep(i,n) for ((i)=1;(i)<=(n);(i)++)
#define rep0(i,n) for ((i)=0;(i)<(n);(i)++)
using namespace std;
long long n,m,s,i;
vector<int> ans;
long long gcd(long long x,long long y){
if(!y) return x;
long long t=gcd(y,x%y);
s+=x/y;return t;
}
void print(long long x,long long y){
if(!x){
while(y--)ans.push_back(2);
return;
}
if(!y){
while(x--)ans.push_back(1);
return;
}
if(x>y){
while(x>=y){
ans.push_back(3);
x-=y;
}
}
else{
while(x<=y){
ans.push_back(4);
y-=x;
}
}
print(x,y);
}
int main(){
srand(time(NULL));
cin>>n;
for(;;){
m=(rand()+rand()*65536ll+rand()*65536ll*65536ll/*+rand()*65536ll*65536ll*65536ll*/)%n;
s=0;
long long g=gcd(n,m);
if(s+g>130) continue;
print(n,m);
printf("%d\n",ans.size());reverse(ans.begin(),ans.end());
rep0(i,ans.size())printf("%d\n",ans[i]);
return 0;
}
return 0;
} | #include<bits/stdc++.h>
//#pragma GCC optimize "trapv"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define lll __int128
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll umap<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define ms(a,x) memset(a,x,sizeof(a))
#define random(l,r,T) uniform_int_distribution<T>(l,r)(rng)
//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
template<typename T> istream& operator >>(istream &in,vector<T> &v){ for(auto &x:v) in>>x; return in;}
template<typename T> ostream& operator <<(ostream &out,vector<T> &v){ for(auto &x:v) out<<x<<' '; return out;}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){ in>>p.first>>p.second; return in;}
template<typename T1,typename T2> ostream& operator <<(ostream &out,pair<T1,T2> &p){ out<<p.first<<' '<<p.second; return out;}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
int32_t main()
{
clock_t clk = clock();
fast_az_fuk
string s; cin>>s; deque<char> ans; ans.pb(s[0]); bool back=1; if(s[0] == 'R') {ans.pop_back();} s.erase(s.begin());
for(char c : s){
if(c == 'R') back = 1 - back;
else {
if(back){
ans.pb(c);
}
else{
ans.pf(c);
}
}
}
if(!back) reverse(all(ans));
string final; ll n = ans.size(); final += '?';
for(ll i=0;i<n;i++){
if(final.back() == ans[i]){
final.pop_back();
}
else final += ans[i];
}
final.erase(final.begin());
cout<<final;
// cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
return 0;
} |
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define all(container) (container).begin(), (container).end()
#define sz(container) static_cast<int>((container).size())
using namespace std;
class Solver {
public:
void solve(istream& in, ostream& out) {
out.precision(20);
int tests;
cin >> tests;
for (int test = 1; test <= tests; test++) {
solveOne(in, out);
}
}
private:
void solveOne(istream& in, ostream& out) {
const string t = "atcoder";
string s;
cin >> s;
if (t < s) {
out << 0 << endl;
return;
}
string worst = s;
sort(all(worst));
reverse(all(worst));
if (worst <= t) {
out << -1 << endl;
return;
}
int answer = sz(s);
int n = sz(s);
int m = min(sz(s), sz(t));
for (int to = 0; to < m; to++) {
for (int from = 0; from < n; from++) {
swap(s[to], s[from]);
if (t < s) {
answer = min(answer, abs(to - from));
}
swap(s[to], s[from]);
}
}
cout << answer << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solver solver;
solver.solve(cin, cout);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n, m;
string s, t="atcoder";
int solve(int w) {
string p = s;
int ans = 0;
for (int i=0; i<w; ++i) {
int ok = 0;
for (int j=i; j<n; ++j) {
if (p[j]==t[i]) {
for (int k=j; k>i; --k) swap(p[k],p[k-1]);
ans += j-i;
ok = 1;
break;
}
}
if (!ok) return 100000;
}
for (int i=w; i<n; ++i) {
auto tmp = p;
for (int k=i; k>w; --k) swap(p[k],p[k-1]);
if (p>t) return ans+i-w;
p = tmp;
}
return 100000;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> s;
n = s.size();
m = t.size();
int ans=1e9;
for (int i=0; i<min(n,m-1); ++i) {
ans = min(ans, solve(i));
}
if (ans>1000) ans=-1;
cout << ans << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
const ll mod = 1e9+7;
main(){
ll a,b;cin>>a>>b;
ll now = 2*a + 100;
cout<<now-b;
} | /*
I am the Best of The World
Nafis_Cruciatus
*/
#include<bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
is;*/
#define fb find_by_order
#define ok order_of_key
typedef long long ll;
typedef int ii;
#define INF 10000000000000007
#define pi 3.141592654
#define T while(t--)
#define for2(i,a,b) for(i=a;i>=b;i--)
#define for3(i,a,b) for(i=a;i<=b;i=i+2)
#define for1(i,a,b) for(i=a;i<=b;i=i+1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define si set<ll>
#define se multiset<ll>
typedef long double ld;
typedef vector<ll> vi;
#define all(v) sort(v.begin(),v.end())
#define all1(v) sort(v.rbegin(),v.rend())
#define pf(uuv) printf("%lld\n",uuv)
#define pf1(uu) printf("%.20Lf\n",uu)
ii main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll t=1;
//cin>>t;
while(t--){
ll a,b; cin>>a>>b;
cout<<((2*a)+100)-b<<"\n";
}
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;
int used[111];
vector<int> v[111];
void dfs(int x){
used[x]=1;
for(auto to: v[x]){
if(used[to])continue;
dfs(to);
}
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin>>n;
string s[n];
rep(i,n)cin>>s[i];
rep(i,n)rep(j,n){
if(s[i][j]=='1'){
v[j].push_back(i);
}
}
long double ans = 0;
rep(i,n){
rep(i,n)used[i]=0;
dfs(i);
int cnt = 0;
rep(i,n)cnt+=used[i];
ans+= (long double)1.0 / cnt;
}
cout<<fixed<<setprecision(15)<<ans<<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;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define rsz resize
#define pcnt __builtin_popcount
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x) memset(c,x,sizeof c)
#define No(x) cout<<(x?"NO":"No")<<endl;
#define Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <r; i++)
#define fored(i, l, r) for(int i = r-1; i >=l; i--)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
// for greater use greater<T>
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out << x << " ";
}
out << "\n";
return out;
}
template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out + x;
}
return out;
}
template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto &x : vec) {
in >> x;
}
return in;
}
template<class T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<class T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
#ifdef LOCAL
void debug() { cerr << endl; }
template<class T, class... Args>
void debug(const T &t, const Args &... args) {
cerr << t << " ";
debug(args...);
}
#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;
inline ll power(ll x, ll y) {
ll res = 1;
x = x;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN = 2e5 + 5;
void solve() {
int n;
cin>>n;
vector<vi>a(n,vi(n));
forn(i,n){
string s;
cin>>s;
forn(j,n){
a[i][j]=s[j]-'0';
}
a[i][i]=1;
}
forn(k,n)forn(i,n)forn(j,n){
a[i][j]|=a[i][k]&&a[k][j];
}
long double ans=0;
forn(i,n){
int d=0;
forn(j,n){
d+=a[j][i];
}
ans+=(long double)1/d;
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1, tc = 1;
// cin >> t;
while (t--) {
// cout<<"Case #"<<tc<<": " ;
solve();
tc++;
}
#ifdef LOCAL
cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
return 0;
}
//look if it requires ll |
/*
problem :
algorithm :
created : Programmer_juruo
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 2000005
#define M 2005
#define lowbit(x) x & (-x)
#define F1(i, n) for(int i = 1; i <= n; i++)
#define F(i, a, n) for(int i = a; i <= n; i++)
#define F0(i, n) for(int i = 0; i < n; i++)
#define dbg(x) cout << #x << ":" << x << endl;
#define se second
#define fi first
#define y1 juruo
#define mp make_pair
#define pb push_back
#define arr vector<int>
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
typedef pair <int, int> pii;
typedef priority_queue <int> pq;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int p = 107;
const int P = 100007;
const int Mod = 1e9+7;
int n, m;
void work() {
cin >> n >> m;
cout << (m-1) + (100*m+n-1) / n <<endl;
}
signed main() {
//freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
int T = 1;
//cin >> T;
while(T--) {
work();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll MOD = 998244353;
#define rep(i,e) for(ll i = 0; i < e; i++)
#define endl
#define dbg(x) cout<<x<<ln
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int main()
{
fast_cin();
ll a[3];
rep(i,3)
{
cin>>a[i];
} sort(a,a+3);
cout<<a[2]+a[1];
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
string S;
cin >> S;
queue<int> I;
for (ll i = 0; i < N; ++i) {
if (S[i] == '1') {
I.emplace(i);
}
}
string T;
cin >> T;
vector<int> E;
for (ll i = 0; i < N; ++i) {
if (T[i] == '1') {
E.emplace_back(i);
}
}
ll E_size = E.size();
ll I_size = I.size();
if ((E_size - I_size) % 2 == 1) {
cout << -1 << endl;
return 0;
}
ll ans = 0;
ll I_first;
ll I_second;
for (auto E_num : E) {
if (I.empty()) {
cout << -1 << endl;
return 0;
}
I_first = I.front();
I.pop();
while (I_first < E_num) {
if (I.empty()) {
cout << -1 << endl;
return 0;
}
I_second = I.front();
I.pop();
ans += I_second - I_first;
if (I.empty()) {
cout << -1 << endl;
return 0;
}
I_first = I.front();
I.pop();
}
ans += I_first - E_num;
}
while (!I.empty()) {
I_first = I.front();
I.pop();
I_second = I.front();
I.pop();
ans += I_second - I_first;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll LLINF = 1LL << 62;
int mod = 1000000007;
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
string S, T; cin >> S >> T;
//1101100011100
//0100101010100
queue<int> T_one;
ll ans = 0;
rep(i, N){
//cout << i << endl;
if(T[i] == '1') T_one.push(i);
if(S[i] == '1' && T_one.empty()){
int old_i = i;
i++;
while(i < N && S[i] == '0'){
if(T[i] == '1') T_one.push(i);
i++;
}
if(T[i] == '1') T_one.push(i);
ans += i-old_i;
}
else if(S[i] == '1'){
int aim = T_one.front();
T_one.pop();
ans += i-aim;
}
}
if(T_one.empty()) cout << ans << endl;
else cout << -1 << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fin(a) { cout << a << endl; return 0; }
#define debug(a) { cerr << #a << ": " << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using ld = long double;
using P = pair<int,int>;
using Pl = pair<lint,lint>;
template<class T> using V = vector<T>;
template<class T> using priq = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return ((a)+(b)-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
template<class T> void YesorNo(T x) { printf(x ? "Yes\n" : "No\n"); }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;
template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
return out << '{' << p.first << ',' << p.second << '}';
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
for(auto &&e : v) in >> e;
return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
for(const auto &e : v) out << e << ' ';
return out;
}
/*----------------------------------------------------------------------------------------------------*/
template<class T>
vector<T> Dijkstra(int s, const vector<vector<pair<T,int>>> &g, vector<int> prev = {}) {
using pi = pair<T,int>;
const T inf = numeric_limits<T>::max()/2;
vector<T> dist(g.size(), inf);
dist[s] = 0;
prev.assign(dist.size(), -1);
priority_queue<pi, vector<pi>, greater<pi>> que;
que.emplace(0,s);
while(!que.empty()) {
P p = que.top(); que.pop();
int now = p.second;
if(dist[now] < p.first) continue;
for(P np : g[now]) {
int next = np.second;
if(dist[next] > dist[now] + np.first) {
prev[next] = now;
dist[next] = dist[now] + np.first;
que.emplace(dist[next], next);
}
}
}
return dist;
}
const int inf = INF;
int main(){
int n, m;
cin >> n >> m;
V<V<P>> g(n*2);
rep(i,m) {
int a, b, c;
cin >> a >> b >> c;
a--; b--;
g[a].emplace_back(c,b+n);
g[a+n].emplace_back(0,a);
}
rep(i,n) {
auto dist = Dijkstra(i,g);
int ans = dist[i+n];
if(ans == inf) ans = -1;
cout << ans << endl;
}
} |
// Problem : D - Takahashi Unevolved
// Contest : AtCoder - AtCoder Beginner Contest 180
// URL : https://atcoder.jp/contests/abc180/tasks/abc180_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
/*ヽ`、ヽ``、ヽ`ヽ`、、ヽ `ヽ 、ヽ`🌙`ヽヽ`ヽ、ヽ`
ヽ`、ヽ``、ヽ 、``、 `、ヽ` 、` ヽ`ヽ、ヽ `、ヽ``、
ヽ、``、`、ヽ``、 、ヽヽ`、`、、ヽヽ、``、 、 ヽ`、
ヽ``、 ヽ`ヽ`、、ヽ `ヽ 、 🚶ヽ````ヽヽヽ`、、ヽ`、、ヽ*/
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex< ld > cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector< int > vi;
typedef vector< ld > vd;
typedef vector< ll > vl;
typedef vector< pi > vpi;
typedef vector< pl > vpl;
typedef vector< cd > vcd;
#define l00p(i, a, b) for (int i=a; i<(b); i++)
#define loop(i, a) for (int i=0; i<(a); i++)
#define rep1(i, a, b) for (int i = (b)-1; i >= a; i--)
#define rep(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define vt vector
#define bug(x) cout<<#x<<"="<<x<<endl;
#define sz(x) (int)(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()
#define ins insert
//#define MOD 998244353
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ld pii=3.14159265359;
const int MOD = 1000000007;
const char cl = '\n';
const ll INF = 1e18;
const int MX = 100001; //check the limits, dummy
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll x,y,a,b;
cin>>x>>y>>a>>b;
y--;
ll cnt=0;
while((ll)(x)<(ll)(b/(a-1)) && y>x){
cnt++;
x*=a;
// bug(x);
}
// cout<<cnt<<cl;
cnt+=(y-x)/b;
cout<<cnt;
return 0;
}
// read the question correctly (ll vs int)
// template by bqi343
|
#include <bits/stdc++.h>
#include <iostream>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main() {
vector<int> tileID(50 * 50);
vector<int> steped(50 * 50);
int x, y;
string ans = "";
cin >> y >> x;
rep(i,50 * 50) {
cin >> tileID[i];
}
steped[0] = tileID[50 * y + x];
int i = 0;
while(true) {
steped[i] = tileID[50 * y + x];
if (*find(steped.begin(), steped.end(), tileID[50 * (y - 1) + x]) != tileID[50 * (y - 1) + x] && y != 0) {
y -= 1;
ans += 'U';
} else if (*find(steped.begin(), steped.end(), tileID[50 * y + x - 1]) != tileID[50 * y + x - 1] && x != 0) {
x -= 1;
ans += 'L';
} else if (*find(steped.begin(), steped.end(), tileID[50 * (y + 1) + x]) != tileID[50 * (y + 1) + x] && y != 49) {
y += 1;
ans += 'D';
} else if (*find(steped.begin(), steped.end(), tileID[50 * y + x + 1]) != tileID[50 * y + x + 1] && x != 49) {
x += 1;
ans += 'R';
} else {
break;
}
i++;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/modint>
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define INF 2000000000000000000
#define ll long long
#define ld long double
#define pll pair<ll, ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
//==================param==========
ll depth = 10;
//=================================
ll si, sj;
vector<vector<ll>> board(50, vector<ll>(50));
vector<vector<ll>> point(50, vector<ll>(50));
map<ll, bool> already;
vector<char> way_char = {'R', 'D', 'L', 'U'};
vector<vector<ll>> ways = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
stack<ll> max_ans;
stack<ll> ans;
ll max_point = 0;
ll now_point = 0;
clock_t start;
void get_input() {
cin >> si >> sj;
for (ll i = 0; i < 50; ++i) {
for (ll j = 0; j < 50; ++j) {
cin >> board.at(i).at(j);
}
}
for (ll i = 0; i < 50; ++i) {
for (ll j = 0; j < 50; ++j) {
cin >> point.at(i).at(j);
}
}
already[board.at(si).at(sj)] = true;
}
bool can_move(ll way) {
ll nh = si + ways.at(way).at(0), nw = sj + ways.at(way).at(1);
if (nh < 0 || nw < 0 || nh >= 50 || nw >= 50) {
return false;
}
if (already[board.at(nh).at(nw)]) {
return false;
}
return true;
}
void make_move(ll way) {
si += ways.at(way).at(0), sj += ways.at(way).at(1);
already[board.at(si).at(sj)] = true;
ans.push(way);
now_point += point.at(si).at(sj);
if (now_point > max_point) {
max_ans = ans;
max_point = now_point;
}
}
void rev_move() {
ll way = ans.top();
ans.pop();
now_point -= point.at(si).at(sj);
already[board.at(si).at(sj)] = false;
si -= ways.at(way).at(0), sj -= ways.at(way).at(1);
}
ll cnt = 0;
bool fin = false;
void dfs() {
if (fin) {
return;
}
cnt += 1;
if (cnt % 1000 == 0) {
clock_t end = clock();
ld time = (ld)(end - start) / CLOCKS_PER_SEC * 1000.0;
if (time > 1900) {
fin = true;
return;
}
}
for (ll i = 0; i < 4; ++i) {
if (can_move(i)) {
make_move(i);
dfs();
rev_move();
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
start = clock();
get_input();
dfs();
string s_ans = "";
while (!max_ans.empty()) {
s_ans += way_char.at(max_ans.top());
max_ans.pop();
}
reverse(rng(s_ans));
cout << s_ans << "\n";
}
|
#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
int k = ri();
int m = ri();
auto add = [&] (int &a, int b) {
a += b;
if (a >= m) a -= m;
};
std::vector<std::vector<int> > dp(n + 1);
dp[0] = { 1 };
for (int i = 1; i < n; i++) {
dp[i].resize(dp[i - 1].size() + k * i);
for (int j = 0; j < (int) dp[i - 1].size(); j++) {
int cur = dp[i - 1][j];
add(dp[i][j], cur);
if (j + (k + 1) * i < (int) dp[i].size()) add(dp[i][j + (k + 1) * i], m - cur);
}
for (int j = 0; j + i < (int) dp[i].size(); j++) add(dp[i][j + i], dp[i][j]);
}
/*
for (auto i : dp) {
for (auto j : i) std::cerr << j << " ";
std::cerr << std::endl;
}*/
for (int i = 0; i < n; i++) {
auto &r0 = dp[i];
auto &r1 = dp[n - 1 - i];
int res = 0;
for (int j = 0; j < (int) r0.size() && j < (int) r1.size(); j++) res = (res + (int64_t) r0[j] * r1[j]) % m;
res = ((int64_t) res * (k + 1)) % m;
if (--res < 0) res += m;
printf("%d\n", res);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define endl '\n'
#define D(x) cerr << #x << " = " << (x) << '\n'
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
const int N = 102;
int n, k, mod;
int dp[N][N * N * N];
int acc[N];
int main()
{
cin >> n >> k >> mod;
dp[0][0] = 1;
int mxs = k * n * (n - 1) / 2;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < i; j++)
acc[j] = 0;
for(int j = 0; j <= mxs; j++) {
acc[j % i] += dp[i - 1][j];
if(j - (k + 1) * i >= 0) {
acc[j % i] -= dp[i - 1][j - (k + 1) * i];
}
acc[j % i] %= mod;
dp[i][j] = (acc[j % i] % mod + mod) % mod;
}
}
for(int m = 1; m <= n; m++) {
int ans = -1;
for(int sum = 0; sum <= mxs; sum++) {
ans += 1LL * dp[m - 1][sum] * dp[n - m][sum] % mod * (k + 1) % mod;
ans %= mod;
}
ans = (ans + mod) % mod;
cout << ans << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pr(pair<ll,ll> p){
cout << "[" << p.ff << ", " << p.ss << "]\n";
}
void pr(set<pair<ll,ll>> s){
for(auto u : s){
pr(u);
}
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
ll T = 1, i, j;
//cin >> T;
while (T--) {
ll n;
cin >> n;
string s, t;
cin >> s >> t;
ll ca = 0;
for(auto u : s){
ca += (u - '0');
}
//cout << ca << endl;
for(auto u : t){
ca -= (u - '0');
}
if(ca != 0){
cout << -1;
return 0;
}
vector<ll> a, b;
for(i =0 ; i < n; i++){
if(s[i] == '0'){
a.pb(i);
}
if(t[i] == '0'){
b.pb(i);
}
}
ll ans = 0;
for(i = 0; i < a.size(); i++){
ans += (a[i] != b[i])
; }
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define N 500005
using namespace std;
char A[N], B[N];
set<int>st[2];
int C[N], D[N];
int ans = 0;
int main()
{
int n;
scanf("%d", &n);
scanf("%s", A);
for (int i = 0; i < n; ++i) if (A[i] == '0') C[++C[0]] = i;
scanf("%s", B);
for (int i = 0; i < n; ++i) if (B[i] == '0') D[++D[0]] = i;
if (C[0] != D[0]) puts("-1");
else
{
for (int i = 1; i <= C[0]; ++i) if (C[i] != D[i]) ++ans;
printf("%d\n", ans);
}
} |
#include<bits/stdc++.h>
typedef long double db;
db slope(int x1,int y1,int x2,int y2){return 1.0*(y1-y2)/(x1-x2);}
int main(){
int n,D,H;
std::cin>>n>>D>>H;
db min=INFINITY;
for(int i=0;i<n;++i){
int d,h;
std::cin>>d>>h;
min=std::min(min,slope(d,h,D,H));
}
std::cout<<std::max((db)0,H-min*D)<<std::endl;
return 0;
} | #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
int n, k, m;
cin >> n >> k >> m;
int a, sum=0;
rep(i, n-1){
cin >> a;
sum += a;
}
int m2 = m*n;
int dif = m2 - sum;
if(dif > k) cout << -1 << endl;
else if(dif < 0) cout << 0 << endl;
else cout << dif << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll MOD = 1e9 + 7;
//constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
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;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
int main()
{
ll N;
cin >> N;
vector<ll> A(N);
for (ll i = 0; i < N; i++)
{
cin >> A.at(i);
}
sort(A.begin(), A.end());
ld x;
if (N % 2 == 0)
x = (A.at(N / 2 - 1) + A.at(N / 2)) / 4.0L;
else
x = A.at(N / 2) / 2.0L;
ld ans = N * x + accumulate(A.begin(), A.end(), 0LL);
for (ll i = 0; i < N; i++)
{
ans -= min((ld)A.at(i), 2 * x);
}
ans /= N;
cout << fixed << setprecision(15) << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int lcm(int a, int b){
int lcm = 0, m = min(a, b);
for (int i = m; i > 0; i--){
if (a%i==0 && b%i==0){
lcm = i;
break;
}
}
return lcm;
}
int main(){
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++){
cin >> A[i];
}
sort(A.begin(), A.end());
int LCM = A[0];
for (int i = 1; i < N; i++){
LCM = lcm(A[i], LCM);
}
cout << LCM;
} |
#include <bits/stdc++.h>
#define Mod 998244353
using namespace std;
typedef long long ll;
inline int read() {
int out = 0;
bool flag = false;
register char cc = getchar();
while (cc < '0' || cc > '9') {
if (cc == '-') flag = true;
cc = getchar();
}
while (cc >= '0' && cc <= '9') {
out = (out << 3) + (out << 1) + (cc ^ 48);
cc = getchar();
}
return flag ? -out : out;
}
inline void write(int x, char ch) {
if (x < 0) putchar('-'), x = -x;
if (x == 0) putchar('0');
else {
int num = 0;
char cc[22];
while (x) cc[++num] = x % 10 + 48, x /= 10;
while (num) putchar(cc[num--]);
}
putchar(ch);
}
int fpow(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = 1ll * res * x % Mod;
x = 1ll * x * x % Mod;
y >>= 1;
}
return res;
}
int n, m, ans = 1; char col[1010], s[510][510];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (int i = 2; i <= n + m; i++) col[i] = '.';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i][j] == '.') continue;
if (col[i + j] == '.') col[i + j] = s[i][j];
else if (col[i + j] != s[i][j]) return puts("0"), 0;
}
}
for (int i = 2; i <= n + m; i++) if (col[i] == '.') ans = ans * 2 % Mod;
cout << ans << endl;
return 0;
} | // C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
const LL MOD = 998244353;
int board[1010][1010]; // マスの色(0: 色なし, 1: 赤, 2: 青).
int cnt[1010][3]; // 出現回数(0: 色なし, 1: 赤, 2: 青).
int main(){
// 1. 入力情報.
int H, W;
scanf("%d %d", &H, &W);
rep(i, H){
char c[505];
scanf("%s", c);
rep(j, W){
if(c[j] == 'R') board[i][j] = 1;
if(c[j] == 'B') board[i][j] = 2;
}
}
// 2. 右上から左下に向かって, マスの色を確認.
rep(k, H + W - 1){
rep(i, H + W - 1){
int j = k - i;
if(j >= 0) cnt[k][board[i][j]]++;
else break;
}
}
// rep(i, H + W - 1) printf("n=%d r=%d b=%d\n", cnt[i][0], cnt[i][1], cnt[i][2]);
// 3. 色なしのみをカウント.
// -> 赤, 青が混在している場合は, 着色不可能と判定.
// -> 開始地点, 終了地点も, '.' となる場合があるので, 注意.
LL ans = 1;
int noColor = 0;
rep(i, H + W - 1){
if(cnt[i][1] && cnt[i][2]) ans = 0;
if(!cnt[i][1] && !cnt[i][2]) noColor++;
}
// 4. 塗り方の総数は?
rep(i, noColor) ans <<= 1, ans %= MOD;
// 5. 出力.
printf("%lld\n", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin>>T;
for(int i=0;i<T;i++){
int64_t L,R;
cin>>L>>R;
int64_t A=R-L*2+1;
if(A<1) cout<<0;
else{
cout<<A*(A+1)/2;
}
cout<<endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll l, r;
cin >> l >> r;
ll ans;
if (2 * l <= r)
ans = (r - 2 * l + 2) * (r - 2 * l + 1) / 2;
else
ans = 0;
cout << ans << '\n';
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// loops
#define fora(i,a,b) for(int i=a;i<=b;++i)
#define forb(i,a,b) for(int i=a;i>=b;--i)
#define forc(i,a,b) for(int i=a;i<b;++i)
#define For(x,y) for(auto x : y)
// data structure
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
const int N = 2005;
int a[N];
int n;
map<int,int> mp;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
fora(i,1,n){
cin >> a[i];
}
sort(a+1,a+n+1);
fora(i,1,n){
for(int j = 1;j * j <= a[i];++j){
if (a[i] % j == 0){
mp[j] = __gcd(mp[j],a[i]);
mp[a[i]/j] = __gcd(mp[a[i]/j],a[i]);
}
}
}
int ans = 0;
For(x,mp){
if (x.fi > a[1]) break;
if (x.fi == x.se) ++ans;
}
cout << ans << "\n";
} | #include<bits/stdc++.h>
using namespace std;
bool check(int k, int n, vector<int> &v)
{
vector<int> vk;
for (int x: v)
{
if (x%k == 0)
vk.push_back(x);
if (k > x)
return false;
}
if (vk.size() == 0)
return false;
int G = vk[0];
for (int x: vk)
G = __gcd(G, x);
if (G != k)
return false;
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int i=0; i<n; i++)
cin >> v[i];
set<int> possibles;
for (int i=0; i<n; i++)
{
int x = v[i];
for (int i=1; i*i<=x; i++)
if (x%i == 0)
possibles.insert(i), possibles.insert(x/i);
}
int sum = 0;
for (int x: possibles)
sum += check(x, n, v);
cout << sum << '\n';
}
|
#include <bits/stdc++.h>
#define int long long
using pii=std::pair<int,int>;
using namespace std;
int t, n;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> t;
for(int cases = 0; cases < t; cases++)
{
cin >> n;
string s;
for(int i = 0; i < 3; i++)
cin >> s;
for(int i = 0; i < n; i++)
cout << "0";
for(int i = 0; i < n; i++)
cout << "1";
cout << "0\n";
}
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <stack>
#include <bitset>
#include <map>
#include <cassert>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
//#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("stones.in", "r", stdin);
// freopen("stones.out", "w", stdout);
}
void solve(){
int n, x;
cin >> n >> x;
for(int i=0; i<n; i++){
int a;
cin >> a;
if(a != x)
cout << a << " ";
}
}
signed main(){
fast_io();
// srand(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while(q--)
solve();
}
|
/** Created by: Humberto Yusta
Codeforces User: humbertoyusta
Country: Cuba */
#include<bits/stdc++.h>
using namespace std;
/// Pragmas
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","03") // Optimization flags
//#pragma GCC option("arch=native","tune=native","no-zero=upper") // Enable AVX
//#pragma GCC target("avx2") // Enable AVX
//#pragma comment(linker, "/STACK:1024000000,1024000000") // Increase stack limit
//#pragma GCC target("sse,sse2,sse,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // I don't know what it is
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 1000010;
const int mod = 1000000007;
const int mod2 = 998244353;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 1ll * mod * mod;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long , null_type , less<long long> , rb_tree_tag , tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
if( !e ) return 1;
if( e & 1 ) return qpow(b,e-1) * b % mod;
int pwur = qpow(b,e>>1);
return pwur * pwur % mod;
}
int modinv(int x){ return qpow(x,mod-2); }
bool isprime(int x){
for(int i=2; i*i<=x; i++)
if( x % i == 0 )
return false;
return true;
}
/// My Code -------------------------------------------------
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cout.setf(ios::fixed); cout.precision(10);
srand(time(NULL));
//freopen("a.in","r",stdin);
//freopen("a.in","w",stdout);
vector<int> pr;
for(int i=2; i<=50; i++){
if( isprime(i) )
pr.pb(i);
}
int n;
cin >> n;
vector<int> a(n+2);
for(int i=1; i<=n; i++)
cin >> a[i];
int ans = -1;
for(int i=0; i<(1<<(pr.size())); i++){
int curr = 1;
for(int j=0; j<pr.size(); j++){
if( i & (1<<j) )
curr *= pr[j];
}
bool can = 1;
for(int j=1; j<=n; j++){
if( __gcd( curr , a[j] ) == 1 )
can = 0;
}
if( can ){
if( ans == -1 ) ans = curr;
else ans = min( ans , curr );
}
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
vector<long long> a(n);
for (long long &x : a)
scanf("%lld", &x);
long long ans = LONG_LONG_MAX;
vector<long long> nums = {2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47};
int m = nums.size();
function<void(int, long long)> f = [&](int k, long long h) {
if (k == m)
{
for (long long x : a)
{
if (__gcd(x, h) == 1)
return;
}
ans = min(ans, h);
}
else
{
f(k + 1, h);
f(k + 1, h * nums[k]);
}
};
f(0, 1);
printf("%lld", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)
ll INF = 1ll << 60;
// UnionFind木
const int MAX = 200010;
int par[MAX], c[MAX];
map<int,int> m[MAX];
void init(){
rep(i, 0, MAX){
par[i] = -1;
m[i][c[i]] = 1;
}
}
int root(int x){
if(par[x] < 0){
return x;
}
return par[x] = root(par[x]);
}
int size(int x){
return -par[root(x)];
}
void unite(int x, int y){
int rx = root(x);
int ry = root(y);
if(rx == ry){
return;
}
if(size(rx) < size(ry)){
swap(rx, ry);
}
for(auto [key,value]:m[ry]){
if(m[rx].count(key))
m[rx][key] += value;
else
m[rx][key] = value;
}
par[rx] += par[ry];
par[ry] = rx;
return;
}
bool same(int x, int y){
int rx = root(x);
int ry = root(y);
if(rx < 0 || ry < 0){
if(rx >= 0){
return rx == y;
}
if(ry >= 0){
return ry == x;
}
return false;
}
return rx == ry;
}
int main(){
int n, q;
cin >> n >> q;
rep(i, 0, n){
cin >> c[i];
c[i]--;
}
init();
rep(i, 0, q){
int t, a, b;
cin >> t >> a >> b;
t--;a--;b--;
if(t){
cout << m[root(a)][b] << endl;
}
else
unite(a,b);
}
return 0;
} | #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
static const int MAX_INT = std::numeric_limits<int>::max();
static const long MAX_LONG = std::numeric_limits<long>::max();
static const ll MAX_LL = std::numeric_limits<long long>::max();
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)
///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
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; }
///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////
struct mint {
static const int mod = 1000000007;
long long x; // typedef long long ll;
mint(long long 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(long long 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; }
};
std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x; }
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x; }
void countNb(ll& res) {
ll n, a, b; cin >> n >> a >> b;
// ensure A <= B
if (b < a) {
ll tmp = a;
a = b;
b = tmp;
}
mint N(n), A(a), B(b);
mint nbA = N - (A - 1); nbA = nbA * nbA;
mint nbB = N - (B - 1); nbB = nbB * nbB;
mint nbtotal = nbA * nbB;
// dupulication: inside
mint nbAb = B - (A - 1); nbAb = nbAb * nbAb;
mint nbInside = nbAb * nbB;
// cross
mint cnt = 0;
for (ll alpha = 1; alpha < a; ++alpha) {
mint Alpha(alpha);
mint nbCross = (B - A + Alpha * 2) * 4;
mint nbIn = N - (B + Alpha - 1); nbIn = nbIn * nbIn;
cnt += nbCross * nbIn;
}
mint count = nbtotal - nbInside - cnt;
res = count.x;
}
void countNb2(ll& res) {
mint N, A, B; cin >> N >> A >> B;
ll nbSpace = N.x - A.x - B.x;
mint x3 = (nbSpace >= 0) ? (N-A-B+2)*(N-A-B+1) : 0;
mint x2 = (N-A+1)*(N-B+1) - x3;
mint x1 = x2 * x2;
mint total = (N-A+1)*(N-B+1) * (N-A+1)*(N-B+1);
mint ans = total - x1;
res = ans.x;
}
void Main() {
ll T; cin >> T;
vector<ll> res(T);
for (ll i = 0; i < T; ++i) {
countNb2(res[i]);
}
rep(i, T) {
cout << res[i] << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed << std::setprecision(15);
Main();
double tmp;
cin >> tmp;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
char s[500005],t[500005];
int f[500005][2],v[500005];
int main()
{
int n,cnt=0;
long long ans=0;
scanf("%d",&n);
scanf("%s\n%s",s+1,t+1);
for(int i=1;i<=n;i++)if(s[i]=='1')v[++cnt]=i;
long long now=1;
for(int i=1;i<=n;i++)
{
while(now<=cnt&&v[now]<i)
{
if(now==cnt){puts("-1");return 0;}
ans+=v[now+1]-v[now];
now+=2;
}
if(t[i]=='0')
{
while(now<=cnt&&v[now]<=i)
{
if(now==cnt){puts("-1");return 0;}
ans+=v[now+1]-v[now];
now+=2;
}
}
else
{
if(now>cnt){puts("-1");return 0;}
else ans+=v[now]-i,now++;
}
}
printf("%lld",ans);
return 0;
} |
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
int main()
{
int n;
string s, t;
cin >> n;
cin >> s;
cin >> t;
// 偶奇が同じか判別する
int sSum = 0, tSum = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') sSum++;
if (t[i] == '1') tSum++;
}
if ((sSum - tSum) % 2 == 1 || sSum < tSum) {
cout << -1;
return 0;
}
long ans = 0;
int sNow = 0, tNow = 0;
int sBefore = -1;
while (tNow < n) {
// tの最初を探す
if (t[tNow] == '0') {
tNow++;
continue;
}
else {
// 相方になり得るsを探す
while (sNow < n && s[sNow] == '0')
sNow++;
if (sNow == n) {
cout << -1;
return 0;
}
if (sNow >= tNow && sBefore == -1) {
// 相方として決定
ans += sNow - tNow;
sNow++;
tNow++;
}
else {
// 相殺するものとする
if (sBefore == -1) {
sBefore = sNow;
sNow++;
}
else {
ans += sNow - sBefore;
sBefore = -1;
sNow++;
}
}
}
}
while (sNow < n) {
if(s[sNow] == '0')
{
sNow++;
}
else {
// 相殺
if (sBefore == -1) {
sBefore = sNow;
sNow++;
}
else {
ans += sNow - sBefore;
sBefore = -1;
sNow++;
}
}
}
cout << ans;
return 0;
} |
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long mod=(long long)1e9+7;
long long ans,cnt[3010][3010],n,s[3010],Now;
int main()
{
//freopen("in.txt","r",stdin);
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&Now);
s[i]=s[i-1]+Now;
}
cnt[1][0]=1;
for(int i=1;i<n;i++)
for(int j=i;j>=1;j--)
{
Now=cnt[j][s[i]%(long long)j];
cnt[j+1][s[i]%(long long)(j+1)]=(cnt[j+1][s[i]%(long long)(j+1)]+Now)%mod;
}
for(int j=1;j<=n;j++)
ans=(ans+cnt[j][s[n]%(long long)j])%mod;
printf("%lld",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
int N,K;
int board[50][50];
long long int fact[51];
bool check1[50];
bool check2[50];
int uf[50];
int find(int x)
{
if(uf[x] < 0)
{
return x;
}
return uf[x] = find(uf[x]);
}
bool merge(int x,int y)
{
x = find(x);
y = find(y);
if(x==y)
{
return false;
}
uf[x] += uf[y];
uf[y] = x;
return true;
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
/*
vector <int> v = {1,2,3,4};
queue <vector<int>> que;
set <vector<int>> visited;
visited.insert(v);
que.push(v);
while(!que.empty())
{
vector <int> now = que.front();
que.pop();
vector <int> next = now;
int t = next[0];
next[0] = next[1];
next[1] = t;
if(visited.find(next)==visited.end())
{
visited.insert(next);
que.push(next);
}
next = now;
t = next[2];
next[2] = next[3];
next[3] = t;
if(visited.find(next)==visited.end())
{
visited.insert(next);
que.push(next);
}
/*
next = now;
t = next[0];
next[0] = next[3];
next[3] = t;
if(visited.find(next)==visited.end())
{
visited.insert(next);
que.push(next);
}
next = now;
t = next[1];
next[1] = next[2];
next[2] = t;
if(visited.find(next)==visited.end())
{
visited.insert(next);
que.push(next);
}
}
cout << visited.size() << '\n';
return 0;
*/
cin >> N >> K;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
cin >> board[i][j];
}
}
fact[0] = 1;
for(int i=1;i<=50;i++)
{
fact[i] = ((fact[i-1]%MOD)*(i%MOD)%MOD);
fact[i]%=MOD;
}
long long int res = 1;
memset(uf,-1,sizeof(uf));
for(int i=0;i<N;i++)
{
for(int j=i+1;j<N;j++)
{
bool ok = true;
for(int k=0;k<N;k++)
{
if(board[i][k] + board[j][k] > K)
{
ok = false;
break;
}
}
if(ok)
{
merge(i,j);
}
}
}
for(int i=0;i<N;i++)
{
if(uf[i] < 0)
{
res = ((res%MOD)*(fact[abs(uf[i])]%MOD)%MOD);
res%=MOD;
}
}
memset(uf,-1,sizeof(uf));
for(int i=0;i<N;i++)
{
for(int j=i+1;j<N;j++)
{
bool ok = true;
for(int k=0;k<N;k++)
{
if(board[k][i] + board[k][j] > K)
{
ok = false;
break;
}
}
if(ok)
{
merge(i,j);
}
}
}
for(int i=0;i<N;i++)
{
if(uf[i] < 0)
{
res = ((res%MOD)*(fact[abs(uf[i])]%MOD)%MOD);
res%=MOD;
}
}
cout << res << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
#define time cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
typedef unsigned long long int ull ;
#define int long long
int MOD = 1e7+7 ;
int gcd(int a, int b){
if(b==0)
return a ;
return gcd ( b , a % b) ;
}
int lcm (int a ,int b ){
return (a*b)/gcd(a,b) ;
}
void solve(){
int a; int b ;
cin >> a >> b ;
int c = a+b ;
if(c>=15 && b>=8){
cout << 1 ;
}
else
if(c>=10 && b>=3){
cout << 2;
}
else
if(c>=3){
cout << 3;
}
else{
cout << 4 ;
}
}
int32_t main()
{
fast ; time ;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve() ;
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;
using LL = long long;
using LD = long double;
using ordered_set = tree<LL,null_type,less<LL>,rb_tree_tag,tree_order_statistics_node_update>;
void fastio() {
cin.tie(nullptr);
cin.sync_with_stdio(false);
}
const LL MOD = 998244353;
const LL INF = 1e18;
const LL N = 2e5+5;
#define S second
#define F first
#define vt vector
LL binpow(LL x, LL p, LL m) {
LL res = 1;
x %= m;
while (p) {
if (p&1) {
res = res * x % m;
}
x = x * x % m;
p >>= 1;
}
return res;
}
int main() {
fastio();
LL n, m;
cin >> n >> m;
LL sub = 0;
LL mi = binpow(m, MOD-2, MOD);
LL mp = binpow(m, n-1, MOD);
vt<LL> dmx(m+1, 1);
for (LL d = 2; d <= n; ++d) {
LL tim = n-d+1;
LL res = 0;
mp = (mp*mi) % MOD;
for (LL x = 1; x <= m; ++x) {
//
res = (res + dmx[m-x] % MOD * mp) % MOD;
dmx[m-x] = dmx[m-x]*(m-x) % MOD;
}
LL trm = (res * tim) % MOD;
//cerr << trm << "?";
sub = (sub + trm) % MOD;
}
LL ans = n*binpow(m, n, MOD) % MOD;
ans -= sub;
if (ans < 0) {
ans += MOD;
}
cout << ans << "\n";
} |
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
// your code goes here;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t=1;
//cin>>t;
while(t--)
{
ll n;
cin>>n;
ll prmi=0;
ll a[n];
map<ll,ll>m;
for(ll i=0;i<n;i++)
{
cin>>a[i];
m[a[i]]=1;
if(m[prmi]==0)
{
cout<<prmi<<endl;
}
else{
while(m[prmi]!=0)
{
prmi++;
}
cout<<prmi<<endl;
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
cerr << endl; \
}
void err(istream_iterator<string>) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
#define all(x) (x).begin(), (x).end()
#define coutp(x, y) cout << (x) << " " << (y) << endl
#define coutn(x) cout << (x) << endl
#define coutd(x) cout << setprecision(10) << (x) << endl
/// main函数
int main() {
ll N;
cin >> N;
ll ans = 0;
ll sqsum = 0;
ll sumsq = 0;
ll A;
for (int i = 0; i < N; ++i) {
cin >> A;
sqsum += A * A;
sumsq += A;
}
sumsq *= sumsq;
ans = N * sqsum - sumsq;
coutn(ans);
return 0;
}
|
// time-limit: 2000
// problem-url: https://atcoder.jp/contests/abc185/tasks/abc185_e
#include <bits/stdc++.h>
#define endl "\n"
#define int long long
#define double long double
#define precise \
fixed << setprecision(12)
#define debug(var) \
if (debugging) \
cerr << (#var) << ": " << var << endl
const bool debugging = 1;
const int mod = 1e9 + 7;
using namespace std;
template <class T>
ostream &operator<<(ostream &os, vector<T> V)
{
for (auto v : V)
{
os << v << " ";
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, vector<vector<T>> V)
{
for (int i = 0; i < V.size(); i++)
{
for (int j = 0; j < V[i].size(); j++)
{
os << V[i][j] << " ";
}
os << endl;
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, set<T> S)
{
for (auto s : S)
{
os << s << " ";
}
return os << "";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P)
{
return os << P.first << " " << P.second;
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M)
{
os << endl;
for (auto m : M)
{
os << m << endl;
}
return os << "";
}
const int max_val = 1e3 + 10;
int dp[max_val][max_val];
int N, M;
vector<int> arr1;
vector<int> arr2;
int recur(int ptr1, int ptr2)
{
if (ptr1 == N || ptr2 == M)
{
return (N - ptr1) + (M - ptr2);
}
if (dp[ptr1][ptr2] != -1)
{
return dp[ptr1][ptr2];
}
dp[ptr1][ptr2] = INT64_MAX;
dp[ptr1][ptr2] = min(dp[ptr1][ptr2], 1 + recur(ptr1 + 1, ptr2));
dp[ptr1][ptr2] = min(dp[ptr1][ptr2], 1 + recur(ptr1, ptr2 + 1));
dp[ptr1][ptr2] = min(dp[ptr1][ptr2], (arr1[ptr1] != arr2[ptr2]) + recur(ptr1 + 1, ptr2 + 1));
return dp[ptr1][ptr2];
}
void solve()
{
cin >> N >> M;
arr1.resize(N);
for (int i = 0; i < N; i++)
{
cin >> arr1[i];
}
arr2.resize(M);
for (int i = 0; i < M; i++)
{
cin >> arr2[i];
}
fill_n(&dp[0][0], max_val * max_val, -1);
cout << recur(0, 0) << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int no_of_testcases = 1;
// cin >> no_of_testcases;
while (no_of_testcases-- > 0)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {if (val < 0) val += MOD;}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {return val ? MOD - val : 0;}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {val += r.val;if (val >= MOD) val -= MOD;return *this;}
constexpr Fp& operator -= (const Fp& r) noexcept {val -= r.val;if (val < 0) val += MOD;return *this;}
constexpr Fp& operator *= (const Fp& r) noexcept {val = val * r.val % MOD;return *this;}
constexpr Fp& operator /= (const Fp& r) noexcept {long long a = r.val, b = MOD, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}val = val * u % MOD;if (val < 0) val += MOD;return *this;}
constexpr bool operator == (const Fp& r) const noexcept {return this->val == r.val;}
constexpr bool operator != (const Fp& r) const noexcept {return this->val != r.val;}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {return os << x.val;}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {if (n == 0) return 1;auto t = modpow(a, n / 2);t = t * t;if (n & 1) t = t * a;return t;}
};
const ll INF = 1LL<<60;
struct edge
{
int to;
ll cost;
};
void dijkstra(const vector<vector<edge>> &G, int s , vector<ll> &dist)
{
priority_queue<P,vector<P>,greater<P>> que;
dist[s] = 0;
que.push(P(0,s));
while(!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
int d = p.first;
//if(dist[v] < d) continue;
for(auto e : G[v]) {
if(dist[e.to] > dist[v]+e.cost) {
dist[e.to] = dist[v] + e.cost;
que.push(P(dist[e.to],e.to));
}
}
}
}
using mint = Fp<MOD>;
int main() {
int N,M; cin >> N >> M;
vecl d(N);
REP(i,N) d[i] = INF;
vecvecl dist(N,vecl(N, INF));
vector<vector<edge>> G1(N, vector<edge>());
REP(i,M) {
int a,b; cin >> a >> b;
--a,--b;
ll t; cin >> t;
if(a != b) G1[a].push_back({b,t});
if(a == b) d[a] = min(d[a],t);
}
REP(s,N) {
vector<ll> dist1(20010, INF);
dijkstra(G1,s,dist1);
REP(t,N) {
dist[s][t] = min(dist[s][t],dist1[t]);
}
}
// REP(i,N) {
// REP(j,N) cout << dist[i][j] << " ";
// cout << endl;
// }
REP(s,N) {
ll ans = INF;
REP(t,N) {
if(s!=t) if(dist[s][t] == INF || dist[t][s] == INF) continue; else chmin(ans,dist[s][t] + dist[t][s]);
}
if(ans == INF) {
if(d[s] != INF) cout << d[s] << endl;
else cout << -1 << endl;
} else {
if(d[s] != INF) cout << min(ans,d[s]) << endl;
else cout << ans << endl;
}
}
return 0;
} |
#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;
template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
template<class T> void show_vec(T v) {for (int i=0;i<v.size();i++) cout<<v[i]<<endl;}
template<class T> void show_pair(T p) {cout<<p.first<<" "<<p.second<<endl;}
#define REP(i,n) for(int i=0;i<int(n);i++)
#define ROUNDUP(a,b) ((a+b-1)/b)
#define YESNO(T) cout<<(T?"YES":"NO")<<endl
#define yesno(T) cout<<(T?"yes":"no")<<endl
#define YesNo(T) cout<<(T?"Yes":"No")<<endl
const int INFint = 1 << 29;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
int t;
ll n;
cin >> t >> n;
vector<ll> bac(100 + t,0);
for (int i = 0; i < 100; i++){
bac[(100 + t) * i / 100]++;
}
vector<ll> ans;
for (int i = 1; i < 100 + t; i++){
if (bac[i] <= 0) ans.push_back(i);
}
// show_vec(ans);
ll x = ans.size();
ll r = 100 + t;
n--;
cout << ((n / x) * r) + ans[n % x] << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define M 998244353
#define pb push_back
#define p_q priority_queue
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<pii,ll>
#define all(a) (a).begin(),(a).end()
#define sz(x) (ll)x.size()
#define endl '\n'
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b)) / gcd((a),(b))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rep(i, begin, end) for (int i=begin;i<end;i++)
#define repd(i, begin, end) for (int i=end-1;i>=begin;i--)
#define ini(a,n,b) for(ll int i=0;i<n;i++) a[i]=0;
#define cset(a) __builtin_popcountll(a)
#define hell 1000000007
#define re resize
const int INF=1e18;
const int N=1e2+5;
int powm(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
int pow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=(res*a);
a=(a*a);
b>>=1;
}
return res;
}
vi a[N],vis(N,0),len(N,0);
set<pair<int,int> > s;
void dfs(int node,int par)
{
vis[node]=1;
for(auto j:a[node])
{
if(!vis[j])
{
len[j]=len[node]+1;
s.insert({node,j});
dfs(j,node);
}
else if(j!=par&&len[j]<len[node])
{
s.insert({node,j});
}
}
}
signed main(void)
{ios
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout);
#endif
int n,m;
cin>>n>>m;
vi u(m),v(m);
rep(i,0,m)
{
cin>>u[i]>>v[i];
}
vi c(n+1);
rep(i,1,n+1) cin>>c[i];
vi dir(m,0);
rep(i,0,m)
{
if(c[u[i]]>c[v[i]])
{
dir[i]=1;
}
else if(c[v[i]]>c[u[i]])
{
dir[i]=0;
}
else
{
a[u[i]].pb(v[i]);
a[v[i]].pb(u[i]);
}
}
rep(i,1,n+1)
{
if(!vis[i])
{
dfs(i,0);
}
}
rep(i,0,m)
{
if(c[u[i]]==c[v[i]])
{
if(s.find({u[i],v[i]})!=s.end()) dir[i]=1;
else dir[i]=0;
}
}
rep(i,0,m)
{
if(dir[i]==1) cout<<"->"<<endl;
else cout<<"<-"<<endl;
}
} |
#define MOD 1000000007
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
#include <numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
const int N=1000001;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int l,r;
int64_t ans{0};
array < bool , N > prime;
array < int , N > mob;
vector < int > primes;
cin>>l>>r;
prime.fill(true);
prime[0]=prime[1]=false;
for(int64_t i=2; i<prime.size(); ++i){
if(prime[i]){
primes.push_back(i);
mob[i]=-1;
}
for(int j=0; j<primes.size()&&i*primes[j]<prime.size(); ++j){
prime[i*primes[j]]=false;
if(i%primes[j]==0){
mob[i*primes[j]]=0;
break;
}
else{
mob[i*primes[j]]=mob[i]*mob[primes[j]];
}
}
}
for(int i=max(l,2); i<=r; ++i){
ans-=2*(r/i)-1;
}
for(int i=r; i>0; --i){
int64_t c=r/i-(l-1)/i;
ans-=mob[i]*c*c;
}
cout<<ans<<endl;
}
| #include<iostream>
#include<math.h>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<stdlib.h>
#include<stack>
using namespace std;
#define PI 3.14159265358979
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#include<set>
char s[501][501];
int main(void)
{
int h;
int w;
cin>>h>>w;
rep(i,h)
{
string ss;
cin>>ss;
rep(j,w) s[i][j]= ss[j];
}
ll ans =1;
//対称性チェック
//1色,矛盾,なしのどれか
for(int i=0;i<w;i++)
{
//cout<<i<<endl;
bool r = false;
bool b = false;
for(int j=0;i-j>=0&&j<=h;j++)
{
if(s[j][i-j]=='B') b= true;
if(s[j][i-j]=='R') r = true;
}
if(b==true&&r==true)
{
cout<<0<<endl;
return 0;
}
else if(b==false&&r==false)
{
ans *=2;
ans %=998244353;
}
}
int ii=1;
while(ii<h)
{
bool r = false;
bool b = false;
for(int j=0;ii+j<h&&w-j-1>=0;j++)
{
if(s[ii+j][w-1-j]=='B') b= true;
if(s[ii+j][w-1-j]=='R') r = true;
//cout<<w-1-j<<' '<<i+j<<endl;
}
if(b==true&&r==true)
{
cout<<0<<endl;
return 0;
}
else if(b==false&&r==false)
{
ans *=2;
ans %=998244353;
}
ii++;
}
cout<<ans%998244353<<endl;
} |
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define np next_permutation
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define vii vector<vi>
#define vb vector<bool>
#define pr pair<int,int>
#define vl vector<ll>
#define vs vector<string>
#define us unordered_map<int,int>
#define Mpq priority_queue<int>
#define mpq priority_queue<int,vi,greater<int>>
#define eb emplace_back
#define pr pair<int,int>
#define prl pair<ll,ll>
#define vp vector<pr>
#define vpl vector<prl>
#define mkp make_pair
#define ld long double
#define vii vector<vi>
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
#define ull unsigned long long
#define prr pair<ll,int>
using namespace std;
using vi=vector<int>;
// #include "Bigint.hpp"
const int N=1e6+11;
void solve(){
ll n, cnt=0;
cin >> n;
// if(n==1){
// cout << 2 << '\n';
// return;
// }
for(ll i=1;i<(ll)2e6;i++){
ll diff = n - i*(i-1)/2;
if(diff<0LL) break;
if(!diff or diff % i) continue;
cnt++;
}
cout << 2*cnt << '\n';
}
int main(){
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("test3", "r", stdin);
// // for writing output to output.txt
// // freopen("output4.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int t=1;
while(t--)
solve();
}
/*
n*(n+1)/2 - x = N
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 5
1 2 3 4 5
sum(a0+(n-1)d) = n*a0 + n(n-1)/2 * d = (n/2) * (2*a0 + (n-1)d)
n*a0 + n*(n-1)d
an = a0 + (n-1)d
sn = (n/2)*(2*a0 + (n-1)*d)
*/ | #include<iostream>
using namespace std;
typedef long long ll;
ll n;
int main(){
cin >> n;
ll l = 1,r = 1e10;
while(l < r){
ll mid = l + r + 1 >> 1;
if((1 + mid) * mid / 2 > n + 1){
r = mid - 1;
}else if((1 + mid) * mid / 2 == n + 1){
l = mid;
break;
}else{
l = mid;
}
}
cout << n - l + 1 << endl;
return 0;
} |
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#include<map>
#include<iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define input ios_base::sync_with_stdio(0);cin.tie(0);
const double PI = acos(-1.0);
using namespace std;
//typedef pair<double,double>pdd;
typedef long long ll;
typedef pair<ll, ll>pii;
//typedef complex<double> point;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//char rv[4]={'D','R','L','U'};
const double EPS = 1e-9;
const int N = 50 + 9;
ll mod=998244353 , n,k,a[N][N],vis[N],cnt,ans=1,f[N];
vector<int> adj[N];
void dfs(int u)
{
vis[u] = 1;
cnt++;
for(auto v:adj[u])
{
if(!vis[v])dfs(v);
}
}
int main()
{
//freopen("calc.in","r",stdin);
//freopen("calc.out","w",stdout);
//__builtin_popcount()
input
f[0] = 1;
forr(i,1,50)
{
f[i] = ((ll)i * f[i-1]) % mod;
//cout<<f[i]<<endl;
}
cin>>n>>k;
forr(i,1,n)
forr(j,1,n)
cin>>a[i][j];
forr(i,1,n)
{
forr(j,i+1,n)
{
int c=0;
forr(u,1,n)
{
if(a[u][i] + a[u][j] <= k)c++;
}
if(c == n)
{
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
forr(i,1,n)
{
if(!vis[i])
{
cnt=0;
dfs(i);
ans *= f[cnt];
ans %= mod;
}
}
forr(i,1,n)adj[i].clear();
forr(i,1,n)
{
forr(j,i+1,n)
{
int c=0;
forr(u,1,n)
{
if(a[i][u] + a[j][u] <= k)c++;
}
if(c == n)
{
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
memset(vis,0,sizeof vis);
forr(i,1,n)
{
if(!vis[i])
{
cnt=0;
dfs(i);
ans *= f[cnt];
ans %= mod;
}
}
cout<<ans<<endl;
return 0;
}
| #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define PRINT(a) printf("%.15Lf\n",a);
#define YESNO(a) cout << (a ? "YES" : "NO") << endl
#define YesNo(a) cout << (a ? "Yes" : "No") << endl
#define yesno(a) cout << (a ? "yes" : "no") << endl
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
vector<bool>F(100010, false);
int N;
VI C;
vector<VI>G;
VI ans;
void dfs(int now, int pa) {
bool ok = 0;
if (!F[C[now]]) {
ans.push_back(now + 1);
F[C[now]] = 1;
ok = 1;
}
fore(to, G[now]) {
if (to == pa)continue;
dfs(to, now);
}
if (ok)F[C[now]] = 0;
return;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
C.resize(N);
G.resize(N);
REP(i, N)cin >> C[i];
REP(i, N - 1) {
int a, b; cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(0,-1);
SORT(ans);
fore(i, ans)cout << i << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define REP(i, n) for(int i=0;i<(int)(n);++i)
#define FOR(i,l,h) for(int i=(int)(l);i<=(int)(h);++i)
#define FORD(i,h,l) for(int i=(int)(h);i>=(int)(l);--i)
//#define max(a,b) a>b?a:b
//#define min(a,b) a>b?b:a
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define dump(x) cerr << #x << " = " << (x) << endl
#define ALL(t) (t).begin(),(t).end()
#define ALLR(t) (t).rbegin(),(t).rend()
using namespace std;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
int sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }
const int dx4[4] = {0, 0, 1, -1};
const int dy4[4] = {-1, 1, 0, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
#define MOD 1000000007
///////////////////////////////////////
//////////////////////////////////////////
void test_case(){
string s;cin>>s;
string s1 = s;
int n = s.length();
reverse(ALL(s));
if(s == s1){
cout<<"Yes\n";
}
else{
int l = 0,r= n-1;
for (int i = 0; i < n; ++i)
{
if(s[i] == '0')l++;
else break;
}
for (int i = n-1; i >= 0;--i)
{
if(s[i] == '0')r--;
else break;
}
int n1 = (r+l)/2;
while(l <= n1)
{
if(s[l] != s[r]){
cout<<"No\n";
return ;
}
else{
l++;
r--;
}
}
cout<<"Yes\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//init();
int t = 1;//cin>>t;
while (t--)
{
test_case();
}
return 0;
}
| #include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
int main(){
int j=0,n,a[10];
bool zeroend=false;
cin>>n;
for(int i=0;n>0;i++){
if(n%10!=0){
zeroend=true;
}
if(zeroend){
a[j]=n%10;
j++;
}
n/=10;
}
for(int i=0;i<j/2;i++){
if(a[i]!=a[j-1-i]){
cout<<"No"<<endl;
return 0;
}
}
cout<<"Yes"<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=1e18;
int main(){
int n, m; cin >> n >> m;
vector<vector<bool>> edge(n,vector<bool>(n,false));
int a, b;
REP(i,m){
cin >> a >> b;
a--, b--;
edge[a][b]=true;
edge[b][a]=true;
}
VI dp(1<<n,INF);
REP(i,1<<n){
VI v;
int ii=i;
REP(j,n){
if(ii%2)
v.push_back(j);
ii=ii>>1;
}
int siz=v.size();
int c=0;
REP(j,siz)REP(k,j){
c+=edge[v[j]][v[k]];
}
if(c==siz*(siz-1)/2)
dp[i]=1;
}
int ans=n;
REP(i,1<<n){
for(int j=i;--j&=i;)
dp[i]=min(dp[i],dp[i^j]+dp[j]);
}
cout << dp[(1<<n)-1] << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i = 0;(i) < ((int)(n));++(i))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )
#define test(a) cout<<"line:"<<__LINE__<<"["<<(#a)<<": "<<(a)<<"]"<<endl
constexpr int INF = 1e9+7;
constexpr ll INFL = 9*1e18;
constexpr int dx[4] = { 1, 0, -1, 0 };
constexpr int dy[4] = { 0, 1, 0, -1 };
// constexpr int dx[8] = {1, 1, 0,-1,-1,-1, 0, 1};
// constexpr int dy[8] = {0, 1, 1, 1, 0,-1,-1,-1};
int inline digit(ll num){int tmp = 0;while(num){tmp++;num/=10;}return tmp;} // 桁数
ull inline power(ull a,ull b){ull res = 1;rep(i,b)res *= a;return res;}
template<typename T>inline T SUM(vector<T> vec){return accumulate(all(vec),(T)0);} // vectorの中身を全部足す
template<typename T>inline T digitSum(T num){T sum = 0;while(num){sum+=num%10;num/=10;}return sum;} // 各桁の和
template<typename T>inline T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);} // 最大公約数
template<typename T>inline T lcm(T a, T b){T g = gcd(a,b);return a/g*b;} // 最小公倍数
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T>inline void print(T&& x){cout<<setprecision(32)<<x<<endl;}
string to_oct(int n){string s;while(n){s = to_string(n%8) + s;n /= 8;}return s;}
int main(){
int n;cin>>n;
string s;cin>>s;
string t = "";
int ans = 0;
rep(i,n){
t.push_back(s[i]);
if(t.length() >= 3 && (t.substr(t.length()-3) == "fox")){
t = t.substr(0,t.size()-3);
ans++;
}
}
print(n - ans*3);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int DEBUG = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
int n, m, k;
cin >> n >> m >> k;
vector<int> is_back(n + 1);
for (int i = 0; i < k; i++) {
int p;
cin >> p;
is_back[p] = 1;
}
int c = 0;
for (int i = 0; i < n; i++) {
if (is_back[i]) {
c++;
if (c >= m) {
cout << -1 << endl;
return 0;
}
} else {
c = 0;
}
}
vector<double> xs(n + 1);
vector<double> ys(n + 1);
int l = n;
for (int i = n - 1; i >= 0; i--) {
if (is_back[i]) {
xs[i] = 1.0;
ys[i] = 0.0;
} else {
if (l == n) {
for (int j = i + 1; j <= n - 1; j++) {
xs[i] += xs[j] / m;
ys[i] += ys[j] / m;
}
ys[i] += 1.0;
} else {
xs[i] = xs[l];
ys[i] = ys[l];
for (int j = i + 1; j <= l; j++) {
xs[i] += xs[j] / m;
ys[i] += ys[j] / m;
}
for (int j = i + m + 1; j <= l + m; j++) {
xs[i] -= xs[min(j, n)] / m;
ys[i] -= ys[min(j, n)] / m;
}
}
l = i;
}
}
double a = xs[0];
double b = ys[0];
cout << b / (1.0 - a) << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}
int solve(int r1, int c1, int r2, int c2) {
if (r1 == r2 && c1 == c2) return 0;
if (abs(r1 - r2) + abs(c1 - c2) <= 3) return 1;
if ((r1 + c1) == (r2 + c2)) return 1;
if ((r1 - c1) == (r2 - c2)) return 1;
if (abs((r1 + c1) - (r2 + c2)) <= 6) return 2;
if (abs((r1 - c1) - (r2 - c2)) <= 6) return 2;
if ((abs(r1+c1) - abs(r2+c2)) % 2 == 0) return 2;
if ((abs(r1-c1) - abs(r2-c2)) % 2 == 0) return 2;
return 3;
}
int main2() {
ll r1 = nextLong();
ll c1 = nextLong();
ll r2 = nextLong();
ll c2 = nextLong();
cout << solve(r1, c1, r2, c2) << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define PRINT(a) cout << (a) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';
using ll = long long int;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
int c[200005];
int main(){
int n;
cin >> n;
int ans=0;
REP(i,n){
int p;
cin >> p;
c[p]++;
while(c[ans]!=0)ans++;
cout << ans << endl;
}
}
| #pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template <class T>
void CVEC(const T &v) {
int c = v.size() - 1;
for (int i = 0; i < c; i++) cout << v[i] << ' ';
if (c > -1) cout << v[c];
cout << '\n';
}
#pragma endregion header
int XMAX = 10000;
int YMAX = 10000;
struct Rectangle{
int a, b, c, d;
};
struct Point{
int id, x, y, r;
bool operator<( const Point& right ) const {
return x == right.x ? y < right.y : x < right.x;
}
};
void COUT(Rectangle x){
cout << x.a << " " << x.b << " " << x.c << " " << x.d << endl;
}
int n;
vector<int> area(n, 0);
signed main(){
cin >> n;
vector<Point> Ps;
map<int, Rectangle> ans;
rep(i,n){
int x, y, r; cin >> x >> y >> r;
Ps.push_back({i, x, y, r});
}
sort(ALL(Ps));
/*
rep(i,n){
int a = Ps[i].x;//x
int b = Ps[i].y;//y
int r = Ps[i].r;
cout << a << " " << b << " " << r << endl;
}
*/
vector<vector<bool>> occupied(XMAX/100, vector<bool>(YMAX/100, 0));
vector<bool> used(n, 0);
for(int i = 0; i < XMAX/100; i++){
for(int j = 0; j < YMAX/100; j++){
for(int k = 0; k < n; k++){
int x = Ps[k].x;
int y = Ps[k].y;
int id = Ps[k].id;
if(i * 100 <= x && x < (i + 1) * 100 && j * 100 <= y && y < (j + 1) * 100){
ans[id] = {i * 100, j * 100, (i + 1) * 100, (j + 1) * 100};
used[id] = 1;
occupied[i][j] = 1;
break;
}
}
}
}
for(int id = 0; id < n; id++){
if(!used[id]){
bool flag = false;
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
if(!occupied[i][j]){
ans[id] = {i * 100, j * 100, (i + 1) * 100, (j + 1) * 100};
occupied[i][j] = 1;
flag = true;
break;
}
}
if(flag)break;
}
}
}
rep(i,n){
COUT(ans[i]);
}
} |
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
scanf("%d", &n);
printf("%d", 100-n%10-(n/10%10)*10);
return 0;
} | // #include<vector>
// #include<iostream>
// #include<stdio.h>
// #include<string>
// #include<algorithm>
#include<array>
#include<bits/stdc++.h>
using namespace std;
int main() {
double x;
cin >> x;
int memo = x;
x = 1.0*x / 100;
x = ceil(x)*100;
if ( memo % 100 == 0) {
cout << 100 << endl;
} else
cout << x - memo << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };
char mp[1000][1000];
vector<int> gph[2000];
bool chk[2000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int r, c;
cin >> r >> c;
gph[0].push_back(r);
gph[r].push_back(0);
gph[0].push_back(r + c - 1);
gph[r + c - 1].push_back(0);
gph[r - 1].push_back(r);
gph[r].push_back(r - 1);
gph[r - 1].push_back(r + c - 1);
gph[r + c - 1].push_back(r - 1);
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
cin >> mp[i][j];
if (mp[i][j] == '#') {
gph[i].push_back(r + j);
gph[r + j].push_back(i);
}
}
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < r + c; ++i)
if (!chk[i]) {
queue<int> q;
chk[i] = true;
q.push(i);
bool chk0 = false, chk1 = false;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now < r)
chk0 = true;
else
chk1 = true;
for (int nxt : gph[now])
if (!chk[nxt]) {
chk[nxt] = true;
q.push(nxt);
}
}
if (chk0)
cnt0++;
if (chk1)
cnt1++;
}
cout << min(cnt0, cnt1) - 1;
return 0;
} | #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define mp make_pair
const int p = 1e9 + 7;
int mul(int a, int b) {
return (1LL * a * b) % p;
}
int add(int a, int b) {
int s = (a+b);
if (s>=p) s-=p;
return s;
}
int sub(int a, int b) {
int s = (a+p-b);
if (s>=p) s-=p;
return s;
}
int po(int a, ll deg)
{
if (deg==0) return 1;
if (deg%2==1) return mul(a, po(a, deg-1));
int t = po(a, deg/2);
return mul(t, t);
}
int inv(int n)
{
return po(n, p-2);
}
mt19937 rnd(time(0));
/*
const int N = 1e6 + 5;
vector<int> facs(N), invfacs(N);
void init()
{
facs[0] = 1;
for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
invfacs[N-1] = inv(facs[N-1]);
for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}
int C(int n, int k)
{
if (n<k) return 0;
if (n<0 || k<0) return 0;
return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}*/
struct DSU
{
vector<int> sz;
vector<int> parent;
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == parent[v])
return v;
else
{
auto par = find_set(parent[v]);
parent[v] = par;
return par;
}
}
void union_sets(int a, int b) {
find_set(a);
find_set(b);
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b])
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
}
DSU (int n)
{
parent.resize(n);
sz.resize(n);
for (int i = 0; i<n; i++) make_set(i);
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int H, W; cin>>H>>W;
vector<string> a(H);
for (int i = 0; i<H; i++) cin>>a[i];
DSU dsu(H+W);
for (int i = 0; i<H; i++) for (int j = 0; j<W; j++)
{
bool unite = true;
if (i!=0 && i!=H-1) unite = false;
if (j!=0 && j!=W-1) unite = false;
if (a[i][j]=='#') unite = true;
if (unite) dsu.union_sets(i, H+j);
}
int minn = 1e9;
set<int> vert;
for (int i = 0; i<H; i++) vert.insert(dsu.find_set(i));
minn = min(minn, (int)vert.size() - 1);
set<int> hor;
for (int i = H; i<H+W; i++) hor.insert(dsu.find_set(i));
minn = min(minn, (int)hor.size() - 1);
cout<<minn;
}
|
#include <bits/stdc++.h>
#define ll long long int
#define vll vector<long long int>
using namespace std;
ll MOD = 1e9 + 7;
void solver(){
ll n;
cin>>n;
vll a(n),b(n),c(n);
map<ll,ll> mp;
for(ll i=0;i<n;i++)cin>>a[i];
for(ll i=0;i<n;i++)cin>>b[i];
for(ll i=0;i<n;i++)cin>>c[i];
for(ll i=0;i<n;i++){
mp[b[c[i]-1]]++;
}
ll ans=0;
for(ll i=0;i<n;i++){
ans+=mp[a[i]];
}
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
ll t = 1;
//cin>>t;
while (t--){
solver();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PI;
typedef vector<int> VI;
typedef vector<PI> VPI;
template<class T> bool tmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool tmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define PB(x) push_back(x)
#define MP(x,y) make_pair(x,y)
#define F first
#define S second
#define SZ(x) (int)(x).size()
#define FOR(i,a,b) for(int _n(b),i(a);i<_n;i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define TRAV(a,x) for (auto& a : x)
void setIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int MX = 100005;
int cnt[MX], b[MX], c[MX];
int n;
LL solve() {
LL r = 0;
REP(j, n) r += cnt[b[c[j+1]]];
return r;
}
int main() {
setIO();
cin>>n;
REP(i, n) {
int x; cin >> x;
cnt[x]++;
}
REP(i, n) cin >> b[i+1];
REP(i, n) cin >> c[i+1];
cout<<solve()<<"\n";
return 0;
}
|
#line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}
vector< int > calc(int n, vector< int > ps) {
vector< int > res;
for (auto &p : ps) {
p--;
}
// v : 0-indexed
auto find_idx = [&](int v) {
range(i, 0, n) {
if (ps[i] == v) return i;
}
return -1ll;
};
int k = 0;
auto swap_check = [&](int i) {
return ((i & 1) == (k & 1));
};
auto swap_query = [&](int i) {
// if ((i + k) % 2 != 0) return;
swap(ps[i], ps[i + 1]);
k++;
res.emplace_back(i + 1);
};
rrange(v, 0, n) {
int idx = find_idx(v);
if (idx == v) continue;
while (not swap_check(idx)) {
if (idx == (k&1) + 1) idx--;
swap_query(k & 1);
}
range(i, idx, v) {
swap_query(i);
}
}
if (ps[n-2] != n-2) {
swap_query(n-2);
}
return res;
}
void solve() {
int t = input();
while (t--) {
int n = input();
vector< int > ps(n);
cin >> ps;
auto as = calc(n, ps);
cout << as.size() << endl;
cout << as << endl;
}
}
signed main() {
solve();
}
| #include <bits/stdc++.h>
using namespace std;
int n;
int a[505];
int ans[250005],len;
int p=1;
void doSwap(int pos) {
if(p%2==1) {
int x=rand()%2;
if(2*x+1==pos || 2*x+2==pos) {
x^=1;
}
swap(a[2*x+1],a[2*x+2]);
ans[++len]=2*x+1;
p++;
}else {
if(pos==2 || pos==3) {
swap(a[4],a[5]);
ans[++len]=4;
p++;
}else {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
}
}
void sortSwap4() {
if(a[4]==4) {
return;
}
int pos=0;
for(int j=1;j<=4;j++) {
if(a[j]==4) {
pos=j;
break;
}
}
if(pos==1) {
if(p%2==0) {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
swap(a[1],a[2]);
ans[++len]=1;
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
p++;
}else if(pos==2) {
if(p%2==1) {
swap(a[3],a[4]);
ans[++len]=3;
p++;
}
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
}else if(pos==3) {
if(p%2==0) {
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
}else {
swap(a[3],a[4]);
ans[++len]=3;
p++;
}
}
}
int main() {
srand(time(0));
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
}
if(n==2) {
if(a[1]!=1) {
swap(a[1],a[2]);
printf("1\n1\n");
}else {
printf("0\n\n");
}
continue;
}
len=0;
p=1;
for(int i=n;i>=5;i--) {
int pos=0;
for(int j=1;j<=n;j++) {
if(a[j]==i) {
pos=j;
break;
}
}
if((pos%2==0 && p%2==1) || (pos%2==1 && p%2==0)) {
doSwap(pos);
}
//printf("Value at position %d: %d\n",pos,a[pos]);
for(int j=pos;j<i;j++) {
swap(a[j],a[j+1]);
ans[++len]=j;
p++;
}
//printf("Stage 1 level %d: ",i);
}
//Sort 1-4
if(n>=4) {
sortSwap4();
}
//Sort 1-3
while(a[1]!=1 || a[2]!=2 || a[3]!=3) {
if(p%2==1) {
swap(a[1],a[2]);
ans[++len]=1;
p++;
}else {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
}
printf("%d\n",len);
for(int i=1;i<=len;i++) {
printf("%d ",ans[i]);
}
printf("\n");
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
const int N=505;//点数
const int M=5e5+5;//边数
int End[M],Last[N],Next[M],Len[M],e;//链式前向星
void add_edge(int x,int y,int z)
{
End[++e]=y;
Next[e]=Last[x];
Last[x]=e;
Len[e]=z;
}
int dis[N],cnt[N];//cnt记录各个点入队次数
queue<int> q;
bool inq[N];//inq标记各个点是否在队列中
bool spfa(int s)
{
for(int i=1;i<=n;i++) dis[i]=-1e9;//最短路,初始值为inf
dis[s]=0;
for(int i=1;i<=n;i++)//全部放入队列
{
q.push(i);
inq[i]=true;
cnt[i]++;
}
while(q.size())
{
int x=q.front();
q.pop();
inq[x]=false;
for(int i=Last[x];i;i=Next[i])
{
int y=End[i];
if(dis[y]<dis[x]+Len[i])
{
dis[y]=dis[x]+Len[i];
if(!inq[y])
{
q.push(y);
inq[y]=true;
cnt[y]++;
if(cnt[y]>n+1) return false;//返回 false,意味着无解
}
}
}
}
return true;
}
int s[N],pd[N][N],c[N][N],y[N];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++){
for(int k=1;k<=n;k++){
cin>>c[i][k];
s[i]+=c[i][k];
}
}
for(int i=1;i<=n;i++)add_edge(0,i,0);
for(int i=1;i<=n;i++){
for(int k=i;k<=n;k++){
if(i==k)continue;
//if(s[i]-s[k]<0)continue;
if(pd[i][k])continue;
if((s[i]-s[k])%n!=0){
puts("No");
return 0;
}
add_edge(i,k,-(s[i]-s[k])/n);
add_edge(k,i,-(s[k]-s[i])/n);
pd[i][k]=pd[k][i]=1;
}
}
/* for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_edge(y,x,z);
}*/
if(spfa(1))
{
int minn=1e9;
int maxx=-1e9;
for(int i=1;i<=n;i++){
minn=min(minn,dis[i]);
maxx=max(maxx,dis[i]);
}
for(int i=1;i<=n;i++){
dis[i]-=minn;
}
for(int i=1;i<=n;i++){
y[i]=c[1][i]-dis[1];
}
for(int i=1;i<=n;i++){
for(int k=1;k<=n;k++)
if(c[i][k]-dis[i]!=y[k]){
puts("No");
return 0;
}
}
puts("Yes");
for(int i=1;i<=n;i++)printf("%lld ",dis[i]);
puts("");
for(int i=1;i<=n;i++)printf("%lld ",y[i]);
// for(int i=1;i<=n;i++) printf("%d%c",dis[i],i<n?' ':'\n');
}
else printf("No\n");
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef aa
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(1,1e5);
uniform_int_distribution<int> rng1(0,1);
uniform_int_distribution<int> rng2(1,5e4);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int 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)
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 2e5 + 5;
void solve(){
int n;
cin >> n;
vvi c(n, vi(n));
vi a(n), b(n);
rep(i,0,n){
rep(j,0,n){
cin >> c[i][j];
}
a[i] = c[i][0];
}
rep(j,0,n){
b[j] = c[0][j] - a[0];
}
rep(i,0,n){
rep(j,0,n){
if(c[i][j] != a[i] + b[j])
done("No");
}
}
int x = 0;
int y = *min_element(all(a));
if(y < 0)
x = -y;
y = *min_element(all(b));
if(y < 0)
x = y;
cout << "Yes" << endl;
rep(i,0,n)
cout << a[i] + x << ' ';
cout << endl;
rep(i,0,n)
cout << b[i] - x << ' ';
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
|
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
int main(){
int N; std::cin >> N;
std::vector<std::vector<long long>> ct(3);
{
long long a; char c;
std::vector<int> aa(26);
aa['R' - 'A'] = 0;
aa['G' - 'A'] = 1;
aa['B' - 'A'] = 2;
for(int i{0}, i_len{2 * N}; i < i_len; ++i){
std::cin >> a >> c;
ct[aa[c - 'A']].push_back(a);
}
}
std::vector<int> E, Z, O;
for(int i{0}; i < 3; ++i){
std::sort(std::begin(ct[i]), std::end(ct[i]));
if(!(ct[i].size() % 2)) E.push_back(i);
if(ct[i].size() % 2) O.push_back(i);
if(!ct[i].size()) Z.push_back(i);
}
if(E.size() == 3){
std::cout << 0 << std::endl;
return 0;
}
long long ans{LINF};
{
int a{O[0]}, b{O[1]};
for(int i{0}, i_len{int(ct[a].size())}; i < i_len; ++i){
auto itr{std::lower_bound(std::begin(ct[b]), std::end(ct[b]), ct[a][i])};
if(itr != ct[b].end()) chmin(ans, std::abs(*itr - ct[a][i]));
itr = std::upper_bound(std::begin(ct[b]), std::end(ct[b]), ct[a][i]);
if(itr != ct[b].begin()){
--itr;
chmin(ans, std::abs(*itr - ct[a][i]));
}
}
}
std::vector<std::vector<std::pair<long long, int>>> z(2);
if(!Z.size()){
int e{E[0]};
for(int i{0}, i_len{int(ct[e].size())}; i < i_len; ++i){
for(int j{0}; j < 2; ++j){
int o{O[j]};
auto itr{std::lower_bound(std::begin(ct[o]), std::end(ct[o]), ct[e][i])};
if(itr != ct[o].end()) z[j].emplace_back(std::abs(*itr - ct[e][i]), i);
itr = std::upper_bound(std::begin(ct[o]), std::end(ct[o]), ct[e][i]);
if(itr != ct[o].begin()){
--itr;
z[j].emplace_back(std::abs(*itr - ct[e][i]), i);
}
}
}
}
for(int i{0}; i < 2; ++i){
std::sort(std::begin(z[i]), std::end(z[i]));
z[i].erase(std::unique(std::begin(z[i]), std::end(z[i])), std::end(z[i]));
}
for(int i{0}, i_len{std::min(4, int(z[0].size()))}; i < i_len; ++i){
for(int j{0}, j_len{std::min(4, int(z[1].size()))}; j < j_len; ++j){
if(z[0][i].second != z[1][j].second){
chmin(ans, z[0][i].first + z[1][j].first);
}
}
}
std::cout << ans << std::endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
struct ad{
int left, up, right, down;
};
int time_start;
int Num;
std::vector<ad> ads;
std::vector<std::pair<ad,int>> ads_start;
void in(){
std::cin >> Num;
ads.resize(Num);
ads_start.resize(Num);
rep(i,0,Num-1){
std:: cin >> ads[i].left >> ads[i].up;
ads[i].right = ads[i].left + 1;
ads[i].down = ads[i].up + 1;
ads_start[i].first = ads[i];
std::cin >> ads_start[i].second;
}
}
void make_randomcase(){
}
bool is_collision(int N){
if(ads[N].left<0||ads[N].up<0||ads[N].right>10000||ads[N].down>10000) return true;
rep(i,0,Num-1){
if(i==N) continue;
if( std::max(ads[i].left,ads[N].left) < std::min(ads[i].right,ads[N].right) && std::max(ads[i].up,ads[N].up) < std::min(ads[i].down,ads[N].down) ){
return true;
}
}
return false;
}
int score(){
long double point=0;
rep(i,0,Num-1){
point+=1.0-pow(1-1.0*std::min(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up))/std::max(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up)),2);
}
point *= 1e9;
point /= Num;
return round(point);
}
//ランダムに広げる(広げて得をして、なおかつできるものだけ広げる)
void solve(){
std::vector<std::vector<int>> delta(4,std::vector<int> (4,0));
delta[0][0] = delta[1][1] = -1;
delta[2][2] = delta[3][3] = 1;
std::vector<ad> best_ads(Num);
int bestscore = score();
rep(_,0,44){
rep(i,0,Num-1){
ads[i] = ads_start[i].first;
}
time_start = clock();
while(clock() - time_start < 100000){
int select = random()%Num;
int direction = random()%4;
int stretch = ( 10 - (clock() - time_start) / 10000 ) * ( random()%10 + 1 );
ads[select].left += stretch * delta[direction][0];
ads[select].up += stretch * delta[direction][1];
ads[select].right += stretch * delta[direction][2];
ads[select].down += stretch * delta[direction][3];
if(is_collision(select)||(ads[select].right-ads[select].left)*(ads[select].down-ads[select].up)>ads_start[select].second*std::min((long)10, 7 + ( clock() - time_start ) / 20000) / 10){
ads[select].left -= stretch * delta[direction][0];
ads[select].up -= stretch * delta[direction][1];
ads[select].right -= stretch * delta[direction][2];
ads[select].down -= stretch * delta[direction][3];
}
}
if(bestscore < score()){
bestscore = score();
best_ads = ads;
}
}
ads = best_ads;
}
void out(){
rep(i,0,Num-1){
std::cout << ads[i].left << ' ' << ads[i].up << ' ' << ads[i].right << ' ' << ads[i].down << '\n';
}
}
int main() {
srand((unsigned int)time(NULL));
time_start = std::clock();
in();
solve();
out();
std::cerr << score() << std::endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep( i, n, m ) for(int i = ( n ); i < ( m ); i++)
#define rep_d( i, n, m ) for(int i = ( n ) - 1; i >= ( m ); i--)
#define sort_asc( X ) sort(( X ).begin(), ( X ).end())
#define sort_desc( X ) sort(( X ).begin(), ( X ).end(), greater <>())
typedef long long int ll;
int main(){
double N; cin >> N;
double ans = 0;
rep(i,1,N){
ans+=(double)1/(N-i);
}
printf("%.10lf\n", ans*N);
return 0;
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all> // g++ main.cpp -std=c++17 -I .
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
using ll=long long;
const int inf = INT_MAX / 2; const ll INF = 1LL << 60;
// const int MOD = 1000000007;
const int MOD = 998244353;
// cout << fixed << setprecision(15) << ans << endl;
// using mint = modint1000000007;
////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct BIT {
int n; // 配列の要素数(数列の要素数+1)
vector<T> bit; // データの格納先
BIT(int n_) : n(n_ + 1), bit(n, 0) {}
void add(int i, T x) {
for (int idx = i; idx < n; idx += (idx & -idx)) {
bit[idx] += x;
}
}
T sum(int i) {
T s(0);
for (int idx = i; idx > 0; idx -= (idx & -idx)) {
s += bit[idx];
}
return s;
}
// [l,r) の区間和を取得
T query(int l, int r) { return sum(r - 1) - sum(l - 1); }
int lower_bound(T w) { // a_1 + a_2 + ... + a_x >= w となるような最小の x を求める(ただし a_i >= 0)
if (w <= 0) {
return 0;
} else {
int x = 0, r = 1;
while (r < n) r = r << 1;
for (int len = r; len > 0; len = len >> 1) { // 長さlenは1段下るごとに半分に
if (x + len < n && bit[x + len] < w) { // 採用するとき
w -= bit[x + len];
x += len;
}
}
return x + 1;
}
}
};
int main() {
int n;cin>>n;
vector<ll> a(n), b(n);
rep(i, n) {
cin>>a[i]; a[i]+=i;
}
rep(i, n) {
cin>>b[i]; b[i]+=i;
}
map<int, queue<int>> m;
rep(i, n) {
m[a[i]].push(i);
}
BIT<ll> bit(n);
ll ans = 0;
rep(i, n) {
if(m[b[i]].empty()) {
cout << -1 << endl;
return 0;
}
int p = m[b[i]].front();
m[b[i]].pop();
bit.add(p+1, 1);
ans += i+1 - bit.sum(p+1);
}
cout << ans << endl;
return 0;
} |
/**
11 11 000000 111111
11 11 00 111111
1111 00 11
11 11 00 11
11 11 000000 11 **/
#include<bits/stdc++.h>
using namespace std;
#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define vvi vector<vi>
#define vdb vector<db>
#define vldb vector<ldb>
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)a.size()
#define fast ios::sync_with_stdio(false); cin.tie(0)
bool ckmin(int &a, int b) {return (b < a)? a = b, true : false;}
bool ckmax(int &a, int b) {return (b > a)? a = b, true : false;}
int main() {
//freopen("b.inp", "r", stdin);
//freopen("b.out", "w", stdout);
fast;
int n;
cin >> n;
vi dd(100000);
fto(i, 1, n) {
int x;
cin >> x;
++dd[x];
}
fto(i, 1, n) {
if (dd[i] == 0 || dd[i] > 1) {
cout << "No\n";
return 0;
}
}
cout << "Yes\n";
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 100001
#define mod 1000000007
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
set<int> s;
for (int i = 0; i < n; i++)
{
int k;
cin >> k;
s.insert(k);
}
bool can = true;
if (s.size() != n)
can = false;
int curr = 1;
for (auto const &i : s)
{
if (i != curr)
can = false;
curr++;
}
(can) ? cout << "Yes\n" : cout << "No\n";
return 0;
} |
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a > b) {
swap(a, b);
}
if (b > c) {
swap(b, c);
}
if (a > b) {
swap(a, b);
}
if (c - b == b - a) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| #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"
const ll INF = 1e18;
const ll NEGINF = -1 * INF;
/*NOTES: Take care of ll vs int
Never use an iterator after erasing it
*/
void solve()
{
vector<int> a(3);
for (int i = 0; i < 3; i++)
{
cin >> a[i];
}
sort(a.begin(), a.end());
if (a[1] - a[0] == a[2] - a[1])
{
cout << "Yes" << endl;
}
else
{
cout << "No" << 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 <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>
#include <string.h>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "YES" << endl; \
} \
else \
{ \
cout << "NO" << endl; \
}
#define IFYes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "Yes" << endl; \
} \
else \
{ \
cout << "No" << endl; \
}
#define IFyes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "yes" << endl; \
} \
else \
{ \
cout << "no" << endl; \
}
#define DEBUG_OUTPUT_ARRAY(XXX, ONE) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
}
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "<<< i = " << i << " >>>" << endl; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
} \
}
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << i << " "; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << XXX[i][j] << " "; \
} \
cout << endl; \
}
typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;
bool overflow_checker(long long int input_number1,long long int input_number2){
if(5000000000000000000/input_number1<input_number2){
return true;
}else{
return false;
}
}
const long long int mod = 1000000007;
const long long int INF = 2e18;
const long double PI=3.14159265358979323;
const long long int pl=1100;
const long double eps =0.00001;
long long int N,M,K,res=0;
long long int A[202002],B[200200];
string S[200200],T;
int main(){
cout << fixed << setprecision(18);
cin>>T;
for (long long int i = 0; i < T.size()-3; i++)
{
if(T.substr(i,4)=="ZONe"){
res++;
}
}
cout<<res<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
string N;
cin >> N;
int ans=0;
rep(i,9){
if( (N[i] == 'Z') && (N[i+1] == 'O') && (N[i+2] == 'N') && (N[i+3] == 'e') )ans++;
}
cout << ans << endl;
} |
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
vector<vector<PII>>adj(1000);
int main()
{
ll a,b,x,y;
cin>>a>>b>>x>>y;
for(int i=1;i<=100;i++){
adj[i].pb({100+i,x});
adj[100+i].pb({i,x});
if(i!=1){
adj[i].pb({100+i-1,x});
adj[100+i-1].pb({i,x});
}
}
for(int i=1;i<100;i++){
adj[i].pb({i+1,y});
adj[i+1].pb({i,y});
adj[100+i].pb({100+i+1,y});
adj[100+i+1].pb({100+i,y});
}
vector<ll>dist(1000,INF);
dist[a] = 0;
priority_queue<PII,vector<PII>,greater<PII>>pq;
pq.push({0,a});
while(!pq.empty()){
int v = pq.top().se;
int d = pq.top().fi;
pq.pop();
if(dist[v] != d)continue;
for(auto z:adj[v]){
if(dist[z.fi] >dist[v]+z.se){
dist[z.fi] = dist[v]+z.se;
pq.push({dist[z.fi],z.fi});
}
}
}
cout<<dist[b+100];
}
| #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
//#define int long long
using namespace std;
typedef long long ll;
const int maxn=1007;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int HASH=131;
signed main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
int palou=min(d,2*c);
if(a<b)
{
cout<<(b-a)*palou+c<<endl;
}
else if(a==b) cout<<c<<endl;
else if(a>b)
{
cout<<(a-b-1)*palou+c<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define unused [[maybe_unused]]
#define tempT template<class T>
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl
#define ans_exit(x) { cout << x << endl; exit(0); }
#define ans_return(x) { cout << x << endl; return; }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define r_rep(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++)
#define r_reps(i, n, m) for (ll i = (ll)(m) - 1; i >= (ll)(n); i--)
#define _prarr(a) {for(auto _e:a){cerr << _e << " ";} cerr << endl;}
#define debugarr(a) {cerr << #a << ": "; _prarr(a);}
using ll unused = long long;
using ld unused = long double;
using vl unused = vector<ll>;
using vvl unused = vector<vl>;
using vvvl unused = vector<vvl>;
using lp unused = pair<ll, ll>;
using lmap unused = map<int, int>;
unused constexpr ll MOD = 1e9 + 7;
unused constexpr ll INF = 1 << 29;
unused constexpr ll LINF = 1LL << 61;
unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1};
unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1};
unused inline bool bit(ll b, ll i) { return b & (1LL << i); }
unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; }
unused inline void modadd(ll &a, ll b, ll m = MOD) { a = mod(a + b, m); }
unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); }
unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); }
tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; }
tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; }
ll t;
ll f(ll x) {
return (100 + t) * x / 100;
}
int main(){
ll n; cin >> t >> n;
ll losts = f(100) - 100;
ll x = n / losts - !(n % losts);
ll y = n - x * losts;
vl a;
reps(i, 100 * x, 100 * (x + 2)) {
a.pb(f(i));
}
rep(i, 102) {
if(a[i + 1] - a[i] == 1) continue;
y--;
if(!y) {
cout << a[i] + 1 << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> pip;
const int MAXN = 505;
const int INF = 1000000007;
int A[MAXN][MAXN], B[MAXN][MAXN], min_dist[MAXN][MAXN];
set <pip> dijk;
void update_dijk(int nr, int nc, int nwt)
{
if (nwt < min_dist[nr][nc])
{
dijk.erase(pip(min_dist[nr][nc], pii(nr, nc)));
min_dist[nr][nc] = nwt;
dijk.insert(pip(min_dist[nr][nc], pii(nr, nc)));
}
}
int main(int argc, char const *argv[])
{
int r, c;
cin>>r>>c;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c - 1; ++j)
{
cin>>A[i][j];
}
}
for (int i = 0; i < r - 1; ++i)
{
for (int j = 0; j < c; ++j)
{
cin>>B[i][j];
}
}
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
min_dist[i][j] = INF;
}
}
min_dist[0][0] = 0;
dijk.insert(pip(min_dist[0][0], pii(0,0)));
while (!dijk.empty())
{
pip top = *dijk.begin();
dijk.erase(dijk.begin());
int cwt = top.first, cr = top.second.first, cc = top.second.second;
int nr, nc, nwt;
nr = cr;
nc = cc + 1;
if (nc < c)
{
nwt = cwt + A[cr][cc];
update_dijk(nr, nc, nwt);
}
nr = cr;
nc = cc - 1;
if (nc >= 0)
{
nwt = cwt + A[cr][cc - 1];
update_dijk(nr, nc, nwt);
}
nr = cr + 1;
nc = cc;
if (nr < r)
{
nwt = cwt + B[cr][cc];
update_dijk(nr, nc, nwt);
}
for (int i = 1; i <= cr; ++i)
{
nr = cr - i;
nc = cc;
nwt = cwt + (i + 1);
update_dijk(nr, nc, nwt);
}
}
// for (int i = 0; i < r; ++i)
// {
// for (int j = 0; j < c; ++j)
// {
// cout<<min_dist[i][j]<<" ";
// }
// cout<<"\n";
// }
cout<<min_dist[r - 1][c - 1]<<"\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,dp[100][100];
double cnt,ans;
string s[100],t;
int main(void){
cin>>n;
for(int i=0;i<n;i++){
cin>>s[i];
for(int j=0;j<n;j++){
if(i==j)dp[i][j]=0;
else if(s[i][j]=='0')dp[i][j]=1e9;
else dp[i][j]=1;
}
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)continue;
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}
}
}
for(int i=0;i<n;i++){
cnt=1.0;
for(int j=0;j<n;j++){
if(dp[j][i]!=0&&dp[j][i]!=1e9)cnt+=1.0;
}
ans+=1/cnt;
}
cout<<setprecision(15)<<ans<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
const int maxn = 105;
int n;
Lf ans;
int reach[maxn][maxn];
char s[maxn];
int main(){
scanf("%d", &n);
FOR(i, 0, n){
char buf[maxn]; scanf("%s", buf);
FOR(j, 0, n)
reach[i][j] = buf[j] - '0';
reach[i][i] = 1;
}
FOR(k, 0, n) FOR(i, 0, n) FOR(j, 0, n)
reach[i][j] |= (reach[i][k] & reach[k][j]);
FOR(i, 0, n){
int cnt = 0;
FOR(j, 0, n)
cnt += reach[j][i];
ans += (Lf)1.0 / cnt;
}
printf("%.15Lf\n", ans);
return 0;
}
|
#include <cstdio>
#include <vector>
constexpr int maxn = 2e5+10;
std::vector<int> g[maxn];
int dist[maxn], color[maxn], t = 1, l, r;
bool has[maxn];
void dfs(int u, int p = 0) {
for(int v : g[u]) {
if(v == p) continue;
dist[v] = dist[u] + 1;
dfs(v, u);
}
}
void pre(int u, int p = 0) {
has[u] = u == r;
for(int& v : g[u]) {
if(v != p) {
pre(v, u);
has[u] |= has[v];
if(has[v]) std::swap(v, g[u][0]);
}
}
if(g[u].size() && has[u]) std::swap(g[u][0], g[u].back());
}
void get(int u, int p = 0) {
color[u] = t++;
for(int v : g[u])
if(v != p) get(v, u), t++;
}
int main() {
int n; scanf("%d", &n);
for(int i = 1, a, b; i < n; i++)
scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
dfs(1);
l = 1, r = 1;
for(int i = 1; i <= n; i++)
if(dist[i] > dist[l]) l = i;
dist[l] = 0;
dfs(l);
for(int i = 1; i <= n; i++)
if(dist[i] > dist[r]) r = i;
pre(l);
get(l);
for(int i = 1; i <= n; i++)
printf("%d%c", color[i], " \n"[i==n]);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
struct point {
int x, y, ind;
};
bool comp(point a, point b) {
return a.x < b.x;
}
void solve() {
int n; cin >> n;
vector<point> a(n), b(n);
for (int i = 0 ; i < n; ++i) {
cin >> a[i].x >> a[i].y;
b[i].x = a[i].y; b[i].y = a[i].x;
a[i].ind = i;
b[i].ind = i;
}
sort(a.begin(), a.end(), comp);
sort(b.begin(), b.end(), comp);
vector<pair<int, pair<int, int> > > dist;
for (int i = 0; i < 2; ++i) {
for (int j = n - 1; j >= n - 2; --j) {
int d1 = abs(a[i].x - a[j].x), d2 = abs(b[i].x - b[j].x);
int ind1 = a[i].ind, ind2 = a[j].ind, ind3 = b[i].ind, ind4 = b[j].ind;
if (ind1 > ind2) swap(ind1, ind2);
if (ind3 > ind4) swap(ind3, ind4);
dist.push_back({d1, {ind1, ind2}});
dist.push_back({d2, {ind3, ind4}});
}
}
sort(dist.begin(), dist.end());
int m = dist.size();
if (dist[m - 1].second.first == dist[m - 2].second.first && dist[m - 1].second.second == dist[m - 2].second.second ) {
cout << dist[m - 3].first << endl;
}
else cout << dist[m - 2].first << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
for (int i = 0; i < t; ++i)
{
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define repn(i,n) rep(i,0,n)
const double EPS = 1e-5;
const int MOD = 1e9+7;
const int INF = INT_MAX / 2;
//for(auto &h : H){ cout << h << " ";} cout << endl;
int main(){
int r1,c1,r2,c2;
cin >> r1 >> c1;
cin >> r2 >> c2;
int ans = 3;
if(r1==r2&&c1==c2) ans = 0;
else if(r1+c1==r2+c2 || r1-c1==r2-c2 || abs(r1-r2)+abs(c1-c2)<=3) ans = 1;
else if((r1-r2)%2==(c1-c2)%2) ans=2;
else{
for(int i=-2;i<=2;i++){
for(int j=-2;j<=2;j++){
if((r1+i)+(c1+j)==r2+c2 || (r1+i)-(c1+j)==r2-c2 || abs((r1+i)-r2)+abs((c1+j)-c2)<=3){
ans = 2;
}
}
}
int i,j;
i = 3; j = 0;
if((r1+i)+(c1+j)==r2+c2 || (r1+i)-(c1+j)==r2-c2 || abs((r1+i)-r2)+abs((c1+j)-c2)<=3){
ans = 2;
}
i = 0; j = 3;
if((r1+i)+(c1+j)==r2+c2 || (r1+i)-(c1+j)==r2-c2 || abs((r1+i)-r2)+abs((c1+j)-c2)<=3){
ans = 2;
}
i = -3; j = 0;
if((r1+i)+(c1+j)==r2+c2 || (r1+i)-(c1+j)==r2-c2 || abs((r1+i)-r2)+abs((c1+j)-c2)<=3){
ans = 2;
}
i = 0; j = -3;
if((r1+i)+(c1+j)==r2+c2 || (r1+i)-(c1+j)==r2-c2 || abs((r1+i)-r2)+abs((c1+j)-c2)<=3){
ans = 2;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define flush cout.flush
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);
ll cur = -1;
ll calc(vector<ll> &a, vector<ll> &b) {
ll res = INF * INF;
vector<pl> z;
for (ll i = 0; i < a.size(); ++i) {
if (a[i] == cur) {
cur = -1;
continue;
}
z.push_back({a[i], 0});
}
for (ll i = 0; i < b.size(); ++i) {
z.push_back({b[i], 1});
}
ll last0 = -1, last1 = -1;
sort(z.begin(), z.end());
for (ll i = 0; i < z.size(); ++i) {
if (z[i].second == 0) {
if (last1 != -1 && abs(z[i].first - last1) < res) {
res = min(res, abs(z[i].first - last1));
}
last0 = z[i].first;
} else {
if (last0 != -1) {
res = min(res, abs(z[i].first - last0));
}
last1 = z[i].first;
}
}
cur = last0;
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vector<ll> r, g, b;
for (ll i = 0; i < 2 * n; ++i) {
ll a;
cin >> a;
char c;
cin >> c;
if (c == 'R')r.push_back(a);
if (c == 'B')b.push_back(a);
if (c == 'G')g.push_back(a);
}
if (r.size() % 2 == 0 && b.size() % 2 == 0) {
cout << "0\n";
return 0;
}
ll res;
if (r.size() % 2 == 0) {
res = calc(g, b);
ll tmp = calc(r, g);
res = min(res, tmp + calc(r, b));
cur = -1;
tmp = calc(r, b);
res = min(res, tmp + calc(r, g));
cur = -1;
} else if (b.size() % 2 == 0) {
res = calc(r, g);
ll tmp = calc(b, g);
res = min(res, tmp + calc(b, r));
cur = -1;
tmp = calc(b, r);
res = min(res, tmp + calc(b, g));
cur = -1;
} else {
res = calc(r, b);
ll tmp = calc(g, r);
res = min(res, tmp + calc(g, b));
cur = -1;
tmp = calc(g, b);
res = min(res, tmp + calc(g, r));
cur = -1;
}
cout << res << "\n";
return 0;
} |
#include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl
typedef long long LL;
typedef complex<double> Comp;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int modu = 998244353;
template<class T>
inline void read(T &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
int main() {
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
LL r, x, y;
cin >> r >> x >> y;
LL dis = x*x + y*y, R = r*r;
if (R == 1) cout << (int)ceil(sqrt(dis)) << "\n";
else
if (dis < R) cout << "2\n";
else if (dis == R) cout << "1\n";
else {
if (dis % R) cout << (int)floor(sqrt(dis)/r) + 1 << "\n";
else cout << (int)floor(sqrt(dis)/r) << "\n";
}
return 0;
} | #include <iostream>
#include <iomanip>
#include <fstream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <chrono>
using namespace std;
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
string S, X; cin >> S >> X;
vector<set<int>> dp(N + 1);
dp[N].insert(0);
for (int i = N - 1; i >= 0; i--) {
if (X[i] == 'A') {
for (int r = 0; r < 7; r++) {
if (dp[i + 1].count(10 * r % 7) && dp[i + 1].count((10 * r + S[i] - '0') % 7)) dp[i].insert(r);
}
} else {
for (int r = 0; r < 7; r++) {
if (dp[i + 1].count(10 * r % 7) || dp[i + 1].count((10 * r + S[i] - '0') % 7)) dp[i].insert(r);
}
}
}
cout << (dp[0].count(0) ? "Takahashi" : "Aoki") << endl;
return 0;
}
|
#include<iostream>
#include <bits/stdc++.h>
#include<stdio.h>
#include <string.h>
using namespace std;
#define ll long long
#define INF 1000007
#define MAX 500007
#define EPS 1e-9
#define PI acos(-1.0)
#define nl "\n"
#define F first
#define S second
typedef vector<ll> vi;
typedef priority_queue<ll , vector<ll> , greater<ll> > pqmx;
typedef priority_queue<ll> pqmn;
typedef pair<ll , ll> pi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector< pair<int , int> > vpi;
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,a,n) for(i=a;i<n;i++)
#define yeS(GOOD) GOOD ? cout<<"YES\n" : cout<<"NO\n"
#define all(a) a.begin() , a.end()
#define pb push_back
#define ar array
ll mod = LLONG_MAX;
ll binpow(ll a, ll b){ll res=1;a%=mod;while(b>0){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;}
ll binmul(ll a, ll b){ll res=0;a%=mod;while(b>0){if(b&1)res=(res + a)%mod;a=(a + a)%mod;b>>=1;}return res;}
ll area(pi a, pi b,pi c){return abs(a.F * b.S + b.F * c.S + c.F*a.S - a.S * b.F - b.S * c.F - c.S * a.F);}
ll gcd (ll a,ll b){if(b==0)return a;else return gcd (b, a % b);}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll min(ll a,ll b){if(a < b){return a;}return b;}
ll max(ll a,ll b){if(a > b){return a;}return b;}
double intlog(ll n ,ll base){return (double)log(n)/log(base);}
double DEG_to_RAD(double d) { return d * PI / 180.0; }
double RAD_to_DEG(double r) { return r * 180.0 / PI; }
ll n , t , m , temp , temp2, root , p ,q , k , i , j , r , u , v , w;
int dp[1005][1005];
int solve(int i,int j,vi & a,vi & b){
if(i == n && j == m)return 0;
if(i == n)return solve(i,j+1,a,b) + 1;
if(j == m)return solve(i+1,j,a,b) + 1;
if(dp[i][j] != -1)return dp[i][j];
int res = INT_MAX;
if(a[i] == b[j]){
res = min(solve(i+1,j+1,a,b),res);
}
res = min(res, solve(i+1,j,a,b)+1);
res = min(res, solve(i,j+1,a,b)+1);
res = min(res, solve(i+1,j+1,a,b)+1);
return dp[i][j] = res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" ,stdin);
freopen("output.txt" , "w" , stdout);
#endif
cin>>n>>m>>p>>q;
int dp[n][m];
memset(dp,-1,sizeof(dp));
vpi a;
fr(i , p){
cin>>u>>v;
a.pb({u-1,v-1});
}
fr(i , q){
cin>>u>>v;
dp[u-1][v-1] = 4;
}
int res = 0;
fr(i , a.size()){
int x = a[i].F;
int y = a[i].S;
if(dp[x][y] != 1 && dp[x][y] != 3){
p = x;
q = y;
while(p < n && dp[p][q] != 4){
if(dp[p][q] == 2)dp[p][q] = 3;
else dp[p][q] = 1;
p++;
}
p = x;
q = y;
while(p >= 0 && dp[p][q] != 4){
if(dp[p][q] == 2)dp[p][q] = 3;
else dp[p][q] = 1;
p--;
}
}
if(dp[x][y] != 2 && dp[x][y] != 3){
p = x;
q = y;
while(q < m && dp[p][q] != 4){
if(dp[p][q] == 1)dp[p][q] = 3;
else dp[p][q] = 2;
q++;
}
p = x;
q = y;
while(q >= 0 && dp[p][q] != 4){
if(dp[p][q] == 1)dp[p][q] = 3;
else dp[p][q] = 2;
q--;
}
}
}
fr(i , n){
fr(j , m){
if(dp[i][j] == 1 || dp[i][j] == 2 || dp[i][j] == 3){
res++;
}
// cout<<dp[i][j]<<" ";
}
//cout<<nl;
}
cout<<res<<nl;
#ifndef ONLINE_JUDGE
cout << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s .\n";
#endif
}
| #include<bits/stdc++.h>
#define pb push_back
using namespace std;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
typedef long long ll;
const int mod=998244353;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,int y){x=upd(x+y-mod);}
inline void iadd(int &x,int y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
#define rep(i,l,r) for (int i(l);i<=r;i++)
#define per(i,l,r) for (int i(r);i>=l;i--)
int n,m,a[10010],b[10010],c[110];
bool vis[110];
int dir[110][110];
vector<int> e[110];
void dfs(int x){
vis[x]=1;
for (int y: e[x]){
if (c[y]^c[x]) continue;
if (!vis[y]) dir[x][y]=1,dir[y][x]=-1,dfs(y);
else dir[x][y]=-1,dir[y][x]=1;
}
}
void solve(){
n=read(),m=read();
rep(i,1,m) a[i]=read(),b[i]=read(),e[a[i]].pb(b[i]),e[b[i]].pb(a[i]);
rep(i,1,n) c[i]=read();
rep(i,1,n) if (!vis[i]) dfs(i);
rep(i,1,m){
if (c[a[i]]<c[b[i]]) puts("<-");
else if (c[a[i]]>c[b[i]]) puts("->");
else if (dir[a[i]][b[i]]==1) puts("->");
else puts("<-");
}
}
int main()
{
for (int T=1;T--;) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
const int maxn = 112;
int h, w;
int a[maxn][maxn];
int main() {
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> h >> w;
FOR(i, 1, h) FOR(j, 1, w) cin >> a[i][j];
int mn = a[1][1];
FOR(i, 1, h) FOR(j, 1, w) chkmin(mn, a[i][j]);
int ans = 0;
FOR(i, 1, h) FOR(j, 1, w) ans += a[i][j] - mn;
cout << ans;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ll N, M; cin >> N >> M;
vector<ll> h(N), w(M);
vector<ll> ida(N - 1);
for(ll i = 0; i < N; i++)cin >> h[i];
for(ll i = 0; i < M; i++)cin >> w[i];
sort(h.begin(), h.end());
for(ll i = 0; i < N - 1; i++)ida[i] = h[i + 1] - h[i];
vector<ll> est(N), ost(N);
for(ll i = 0; i < N - 1; i++) {
if(i % 2 == 0) est[i] = ida[i];
else ost[i] = ida[i];
}
for(ll i = 1; i < N - 1; i++) {
est[i] += est[i - 1], ost[i] += ost[i - 1];
}
est.insert(est.begin(), 0);
ost.insert(ost.begin(), 0);
ll nin = 2e9;
for(auto t : w) {
ll idx = lower_bound(h.begin(), h.end(), t) - h.begin();
ll cost = 0;
if(idx % 2 == 0) {
cost += h[idx] - t;
cost += est[idx];
cost += ost[N-1] - ost[idx];
} else {
cost += t - h[idx - 1];
cost += est[idx - 1];
cost += ost[N-1] - ost[idx - 1];
}
nin = min(nin, cost);
}
cout << nin << endl;
} |
#include <iostream>
#include <vector>
using namespace std;
int main() {
int i,j,l;
long long res=1;
cin >> l;
for (int i = 1; i < 12; i++) {
res = (res * (l-i))/i;
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using ull = unsigned long long;
using ll = long long;
using namespace std;
#define pf printf
#define sc scanf
#define nline cout << '\n'
#define dbg(x) \
cout << "debug " << #x << ": " << x << '\n';
#define inp(t) cin >> (t)
#define out(t) cout << (t)
#define FOR(n,x) for(ll i = n; i < x; i++)
#define FOR2(N,X) for(ll j = N; j < X; j++)
#define wh(n) while(n--)
ll fib[52] = {};
bool isPrime[50];
void OJ(){
// file I/O
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll LCM(ll x, ll y){ return x * (y/__gcd(x,y)); }
// returns nCr
ll nCr(int n, int r) {
if(r > n - r) r = n - r;
ll ans = 1;
int i;
for(i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
OJ();
// Start
int n; inp(n);
out(nCr(n-1,11));
nline;
} |
//
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
int main ()
{
ios_base::sync_with_stdio (0); cin.tie (0); cout.tie (0);
int x;
cin >> x;
int n = 100 - x % 100;
cout << n;
}
| #include<bits/stdc++.h>
#define ll long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (int)(x).size()
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define what(x) cout<<#x<<" is "<<x<<endl;
using namespace std;
#ifdef LOCAL//ONLINE_JUDGE
#include "D:\c_c++\template.h"
#else
#define debug(...) 42
#endif
ll powmod(ll a,ll b,ll mod) {
ll res=1;a%=mod;
for(;b;b>>=1){
if(b&1)res=res*a%mod;
a=a*a%mod;
}
return res;
}
void solve(){
int n;
cin>>n;
if(n==1){
cout<<0<<'\n';
}else{
cout<<n-1<<'\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// #ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int tc=1;
// cin>>tc;
while(tc--)solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define int long long
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define endl '\n'
#define pi 3.141592653589793238
#define mp make_pair
#define pb push_back
#define precise cout << fixed << setprecision(20)
#define ff first
#define ss second
#define uniq(v) v.resize(distance(v.begin(), unique(all(v))))
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pair<int, int>>
#define vvi vector<vector<int>>
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\n'
const int mod = 1e9 + 7;
const int INF = 1e9;
const int MAXN = 300005;
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &v)
{
for (T &x : v)
out << x << " ";
return out;
}
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int power(int a, int b)
{
int res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
int gcd(int p, int q)
{
if (p % q == 0)
return q;
else
{
return gcd(q, p % q);
}
}
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;
}
int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
int sub(int a, int b) { return (((a - b) % mod) + mod) % mod; }
void primefactorisation(int n)
{
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
int c = 0;
while (n % i == 0)
{
c++;
n = n / i;
}
cout << i << " " << c << endl;
}
}
if (n > 1)
cout << n << " " << 1 << endl;
}
signed main()
{
fastio;
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
for (int i = 1; i <= n; i++)
cout << 1;
for (int i = 1; i <= n; i++)
cout << 0;
cout << 1;
cout << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MX = 200000;
char s[3][MX + 1], t[MX + 2];
bool check(int n) {
for (int i = 0; i < 3; i++) {
int k = 0;
for (int it = 0; it < 2; it++) {
for (int j = 0; j < 2 * n; j++) {
if (s[i][j] == t[k]) k++;
}
}
if (k < 2 * n + 1) return false;
}
return true;
}
int main() {
int T;
ignore = scanf("%d", &T);
while (T--) {
int n;
ignore = scanf("%d %s %s %s", &n, s[0], s[1], s[2]);
int len = max((2 * n + 1 - 9) / 2, 0);
vector<int> lens = {1, 1, 1, len, 1, 1, 1, len, 1, 1, 1};
for (int mask = 0; mask < (1 << lens.size()); mask++) {
int i = 0;
for (size_t j = 0; j < lens.size(); j++) {
for (int k = 0; k < lens[j]; k++) {
t[i++] = '0' + ((mask >> j) & 1);
}
}
if (check(n)) break;
}
t[2 * n + 1] = 0;
printf("%s\n", t);
}
return 0;
}
|
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/rope>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// typedef tree<long long, null_type, std::less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// typedef trie<std::string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>Trie;
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define rdv(x) long long x; cin>>x;
#define deb(x) cout<<#x<<"="<<x<<endl;
#define inf 1e18
#define endl "\n"
typedef vector<long long> vll;
typedef long long ll;
typedef pair<long long,long long> pii;
typedef long double ld;
mt19937 mrand(random_device{}());
const ll mod=100000000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(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;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
bool prime[200001];
void SieveOfEratosthenes()
{
memset(prime, true, sizeof(prime));
prime[1]=false;
for (int p=2; p*p<=200000; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=200000; i += p)
prime[i] = false;
}
}
return;
}
ll ceil(ll x,ll y)
{
ll ans=x/y;
if(x%y)
ans++;
return ans;
}
void file_i_o(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void YN(bool flag)
{
if(flag)
cout<<"YES";
else
cout<<"NO";
}
#define pi 3.1415926535
// head
void solve()
{
ll n;
cin>>n;
map<ll,vll>mp;
rep(i,0,n)
{
ll x,c;
cin>>x>>c;
mp[c].pb(x);
}
ll l=0,r=0,left=0,right=0;
for(auto it:mp)
{
ll nl=inf,nr=-inf;
for(auto i:it.se)
{
nl=min(nl,i);
nr=max(nr,i);
}
ll ldi=inf,rdi=inf;
//L->L2->R2
rdi=min(rdi,abs(l-nl)+abs(nr-nl)+left);
ldi=min(ldi,abs(l-nr)+abs(nr-nl)+left);
ldi=min(ldi,abs(r-nr)+abs(nr-nl)+right);
rdi=min(rdi,abs(r-nl)+abs(nr-nl)+right);
l=nl;
r=nr;
left=ldi;
right=rdi;
}
cout<<min(left+abs(l),right+abs(r));
cout<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false); //Used to reduce the time
cin.tie(NULL);
cout.tie(NULL);
file_i_o();
ll T=1;
//cin>>T;
//ll temp=T;
while(T--)
{
//cout<<"Case #"<<temp-T<<": ";
solve();
}
cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<" ms\n";
}
| #include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9 + 1, MOD = 1e9 + 7; /// think
const ll LINF = 2e18;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
return 0 <= x && 0 <= y && x < n && y < m;
}
template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
inline void solve() {
int l, r;
cin >> l >> r;
ll ans = 0;
int f = max(0, r - l - l + 1);
ans = f * (f + 1ll) / 2ll;
cout << ans << '\n';
}
main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
class bit {
public:
bit(int n);
bit(T* array, int n);
~bit();
void add(int x, T value);
T get(int x) const; // [0, x]
T get(int x, int y) const; // [x, y]
int lower_bound(T value) const;
int upper_bound(T value) const;
int rlower_bound(T value) const;
int rupper_bound(T value) const;
private:
int size;
int high;
T* data;
};
template <class T> bit<T>::bit(int n) : size(n + 1), high(1 << (31 - __builtin_clz(n))) {
data = (T *)calloc(size, sizeof(T));
}
template <class T> bit<T>::bit(T* array, int n) : bit(n) {
for (int i = 0; i < n; i++) data[i + 1] = array[i];
for (int i = 1; i < n; i++) {
if (i + (i & -i) < size) data[i + (i & -i)] += data[i];
}
}
template <class T> bit<T>::~bit() {
free(data);
}
template <class T> void bit<T>::add(int x, T value) {
for (x++; x < size; x += x & -x) data[x] += value;
}
template <class T> T bit<T>::get(int x) const {
T value = 0;
for (x++; x > 0; x -= x & -x) value += data[x];
return value;
}
template <class T> T bit<T>::get(int x, int y) const {
return get(y) - get(x - 1);
}
template <class T> int bit<T>::lower_bound(T value) const {
int x = 0;
for (int i = high; i > 0; i >>= 1) {
if (x + i < size && data[x + i] < value) value -= data[x += i];
}
return x;
}
template <class T> int bit<T>::upper_bound(T value) const {
return lower_bound(value + 1);
}
template <class T> int bit<T>::rlower_bound(T value) const {
int x = 0;
for (int i = high; i > 0; i >>= 1) {
if (x + i < size && data[x + i] <= value) value -= data[x += i];
}
return x - 1;
}
template <class T> int bit<T>::rupper_bound(T value) const {
return rlower_bound(value - 1);
}
int x[200000];
int y[200000];
int xm[200001];
int ym[200001];
int main() {
int h, w, m, i, j;
long long ans = 0;
vector<pair<int, int>> v;
scanf("%d %d %d", &h, &w, &m);
for (i = 0; i < m; i++) scanf("%d %d", &x[i], &y[i]);
for (i = 1; i <= w; i++) xm[i] = h;
for (i = 1; i <= h; i++) ym[i] = w;
for (i = 0; i < m; i++) {
xm[y[i]] = min(xm[y[i]], x[i] - 1);
ym[x[i]] = min(ym[x[i]], y[i] - 1);
}
for (i = 2; i <= ym[1]; i++) v.push_back(make_pair(xm[i], i));
sort(v.begin(), v.end());
bit<long long> b(w + 2);
for (i = 1, j = 0; i <= h; i++) {
if (i <= xm[1]) {
ans += ym[i];
b.add(ym[i] + 1, 1);
} else {
b.add(0, 1);
}
while (j < v.size() && v[j].first <= i) {
int x = v[j].first;
int y = v[j].second;
ans += b.get(y);
j++;
}
}
printf("%lld\n", ans);
return 0;
}
| # include <bits/stdc++.h>
#define rep(i,l,r)for(int i=(l);i<(r);i++)
# define rrep(i,r,l) for(int i=(r); i>=l; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
typedef long long lint;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define INF ((1LL<<62)-(1LL<<31)) /*オーバーフローしない程度に大きい数*/
#define MOD 1000000007
lint gcd(lint a,lint b){
if(!b)return a;
return gcd(b,a%b);
}
lint lcm(lint a,lint b){
return a*b/gcd(a,b);
}
template< typename T >
struct BinaryIndexedTree {
vector< T > data;
BinaryIndexedTree(int sz) {
data.assign(++sz, 0);
}
T sum(int k) {
T ret = 0;
for(++k; k > 0; k -= k & -k) ret += data[k];
return (ret);
}
void add(int k, T x) {
for(++k; k < data.size(); k += k & -k) data[k] += x;
}
};
void solve() {
int h,w,n;
cin >> h >>w >> n;
vector<int> a(w,h),b(h,w);
rep(i,0,n){
int x,y;
cin >> x >> y;x--,y--;
chmin(a[y],x);
chmin(b[x],y);
}
lint ans=0;
rep(y,0,b[0]) ans+=a[y];
rep(x,0,a[0]) ans+=b[x];
BinaryIndexedTree<int> t(w);
rep(i,0,b[0]) t.add(i, 1);
vector<vector<int>> ends(h+1);
rep(y,0,b[0]) ends[a[y]].push_back(y);
rep(x,0,a[0]){
for(int y:ends[x]) t.add(y,-1);
ans-=t.sum(b[x]-1);
}
cout << ans << endl;
}
signed main() {
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define p pair<long long,long long>
#define endl '\n'
#define iof freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define pi acos(-1)
int fx[]={0,0,1,-1,1,1,-1,-1};
int fy[]={1,-1,0,0,-1,1,-1,1};
const int mxn=1e9+7;
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
int n;
cin>>n;
int m;
cin>>m;
vector<p>vp(m);
for(auto &i:vp)
{
cin>>i.first>>i.second;
}
int k;
cin>>k;
vector<p>vk(k);
for(auto &i:vk)
{
cin>>i.first>>i.second;
}
int mx=0;
for(int mask=0;mask<(1<<k);mask++)
{
map<int,int>mp;
for(int i=0;i<k;i++)
{
if((1<<i)&mask)mp[vk[i].first]++;
else mp[vk[i].second]++;
}
int ans=0;
for(auto i:vp)
{
if(mp[i.first]&&mp[i.second])ans++;
}
mx=max(mx,ans);
}
cout<<mx<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define Output(a) cout << a << endl
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
const int INF = 1<<30;
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
string to2(int bina, int k){
int ans = 0;
for(int i = 0; bina > 0; i++){
ans = ans + (bina % 2) * pow(10, i);
bina /= 2;
}
string result = to_string(ans);
while(result.size() < k){
result = "0" + result;
}
return result;
}
int main(){
ll n, m; cin >> n >> m;
vll a(m), b(m);
rep(i, m) cin >> a[i] >> b[i];
ll k; cin >> k;
vll c(k), d(k);
rep(i, k) cin >> c[i] >> d[i];
ll ans = 0;
rep(bit, (1<<k)){
vll s;
rep(i, k){
if(bit & 1 << i){
s.push_back(1);
}else{
s.push_back(0);
}
}
vll t(n);
rep(i, (int)s.size()){
if(s[i]){
t[c[i]-1] = 1;
}else{
t[d[i]-1] = 1;
}
}
ll com = 0;
rep(i, m){
if(t[a[i]-1] == 1 and t[b[i]-1] == 1) com++;
}
ans = max(com, ans);
}
cout << ans << endl;
return 0;
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>;
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;
int main() {
ll N;
cin >> N;
ll ans = 0;
for(ll M = 1; M <= 10000000; M++){
if(2*N - M*M + M > 0 && (2*N - M*M + M)%(2*M) == 0){
ans++;
}
}
cout << ans*2;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
using Graph = vector<vector<int>>;
int main(){
string S[3];
unordered_map<char,int> lab; int k = 0;
rep(i,3){
cin >> S[i];
rep(j,S[i].size()){
char c = S[i][j];
if(!lab.count(c)){
lab[c] = k; k++;
}
}
}
if(lab.size()>10){
cout << "UNSOLVABLE" << endl; return 0;
}
int a = lab.size(); vector<ll> per;
rep(i,10){
per.push_back((ll)i);
}
do{
ll s[3];
rep(i,3){
s[i] = 0; ll base = 1; int l = S[i].size();
//if(i==2) cout << s[2] << endl;
rep(j,l){
s[i] += base*(per[lab[S[i][l-1-j]]]); base *= 10;
//if(i==2) cout << lab[S[i][l-1-j]] << endl;
}
}
//cout << s[2] << endl;
/*if(per[0]==9&&per[1]==5&&per[2]==6&&per[3]==7&&
per[4]==1&&per[5]==0&&per[6]==8&&per[7]==2){
cout << s[0] << endl;
cout << s[1] << endl;
cout << s[2] << endl;
}*/
if(s[0]+s[1]==s[2]){
bool ch = true;
rep(i,3){
if(per[lab[S[i][0]]]==0) ch = false;
}
if(ch){
rep(i,3){
cout << s[i] << endl;
}
return 0;
}
}
}while(next_permutation(per.begin(),per.end()));
cout << "UNSOLVABLE" << endl;
} |
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int main(){
int x=read,y=read,z=read;
if(x==y||y==z||x==z)printf("%d",x^y^z);
else puts("0");
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(int i=a;i<n;i++)
#define fastin ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define mk make_pair
int main(){
fastin
int a,b,c;
cin>>a>>b>>c;
if(a==b){
cout<<c<<endl;
}
else if(a==c)
cout<<b<<endl;
else if(b==c)
cout<<a<<endl;
else
cout<<"0"<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define debug(x) cerr << #x << ": " << x << '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
const ll INF = 1e16;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
template<class T>
bool chmin(T &a, T b){ if(a > b){ a = b; return 1; } return 0; }
template<class T>
bool chmax(T &a, T b){ if(a < b){ a = b; return 1; } return 0; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;
inline int f(int a, int b, int c, int p, int q, int r) {
return abs(p - a) + abs(q - b) + max(0LL, r - c);
}
vi x(17), y(17), z(17);
inline int f(int from, int to) {
return f(x[from], y[from], z[from], x[to], y[to], z[to]);
}
signed main() {
int n;
cin >> n;
rep(i,n) cin >> x[i] >> y[i] >> z[i];
vector<vvi> dp(1 << n, vvi(n, vi(n, INF)));
rep(i,n-1){
dp[1 << i][i][i] = f(0, i+1) + f(i+1, 0);
}
rep(S, 1 << (n-1)) {
rep(i, n-1) {
if(not bit(S, i)) {
rep(st, n-1){
if(not bit(S, st)) continue;
rep(gl, n-1) {
if(not bit(S, gl)) continue;
chmin(dp[S | 1 << i][st][i],
dp[S][st][gl] + f(gl+1, i+1) - f(gl+1, 0) + f(i+1, 0));
chmin(dp[S | 1 << i][i][gl],
dp[S][st][gl] + f(i+1, st+1) - f(0, st+1) + f(0, i+1));
}
}
}
}
}
int ans = INF;
for(auto S : dp[(1 << (n-1)) - 1]) {
for(auto i : S) {
chmin(ans, i);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
for(int i = 0;i < 10; ++i){
bool check = true;
for(int j = 0;j < s.length()/2; ++j){
if(s[j] != s[s.length() - j -1]){
check = false;
break;
}
}
if(check){
cout << "Yes" << endl;
return 0;
}
s = "0" + s;
}
cout << "No" << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.