code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll n;
int main()
{
cin>>n;
ll a,b,c;
ll mins=1e19;
for(int i=0;i<=60;i++)
{
a=n/pow(2,i);
b=i;
c=n-a*(ll)pow(2,i);
mins=min(mins,(a+b+c));
}
cout<<mins;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// --------------------------------------------------------
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 FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (r)-1; (l) <= i; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((int)(c).size())
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << #x << " = " << (x) << '\n';
using P = pair<ll, ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const int INF = (1 << 30) - 1; // 1073741824 - 1
// static const ll INF = (1LL << 60) - 1; // 4611686018427387904 - 1
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll ans = 0;
REP(i, N) {
ll a;
cin >> a;
ans += max(0LL, a - 10);
}
COUT(ans);
} |
// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)
#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
template<class T>inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;}
template<class T>inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;}
char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示
int main(){
string s; cin >> s;
int f = 1;
REP(i, 0 ,s.size()){
if(i % 2 == 0){
if((s[i] - 'a') < 26&& (s[i] - 'a') >= 0){
continue;
}else{
f = 0;
}
}else{
if((s[i] - 'a') < 26&& (s[i] - 'a') >= 0){
f = 0;
}else{
continue;
}
}
}
if(f){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
} |
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using ll = long long;
ll INF = 2e18;
using namespace std;
int N;
int cnt;
int main() {
cin >> N ;
for(int o = 1; o<N+1; o++){
int tmp = o;
int div = 3;
cnt =0;
if(o==1) {
cout << 1<<" ";
continue;
}
while(tmp % 2 == 0){
tmp /= 2;
cnt++;
}
while(tmp != 1){
while(tmp % div == 0){
tmp /= div;
cnt++;
}
div +=2;
}
/* if(cnt > 1){
cout << cnt + 1 << " ";
}
else cout << cnt + 2 << " ";*/
cout << cnt + 1 << " ";
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mkp make_pair
int dis[2002];
int n,m;
vector<pii>g[2002];
void dij(int s){
for(int i=0;i<2002;i++)dis[i]=1e9;
priority_queue<pii,vector<pii>,greater<pii> >pq;
for(auto x:g[s])
if(dis[x.fi]>x.se)dis[x.fi]=x.se,pq.push(mkp(x.se,x.fi));
while(!pq.empty()){
pii u=pq.top();pq.pop();
if(dis[u.se]<u.fi)continue;
for(auto x:g[u.se])
if(dis[x.fi]>u.fi+x.se)
dis[x.fi]=u.fi+x.se,pq.push(mkp(x.se+u.fi,x.fi));
}
cout<<(dis[s]==1e9?-1:dis[s])<<"\n";
}
int main(){
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v,w;cin>>u>>v>>w;
g[u].push_back(mkp(v,w));
}
for(int i=1;i<=n;i++)dij(i);
} | #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<int,int>;
const int INF=1001001001;
const int mod =1e9+7;
struct Unionfind{
vector<int>d;
vector<map<int,int>>mp;
Unionfind(int n):d(n,-1),mp(n){}
int root(int x){
if(d[x]<0){return x;}
return d[x]=root(d[x]);
}
bool unite(int x,int y){
x=root(x);y=root(y);
if(x==y){return false;}
if(d[x]>d[y]){swap(x,y);}
for(auto p:mp[y]){
mp[x][p.first]+=p.second;
}
mp[y]=map<int,int>();
d[x]+=d[y];
d[y]=x;
return true;
}
bool same(int x,int y){return root(x)==root(y);}
int size(int x){return -d[root(x)];}
};
int main() {
int N,Q;cin>>N>>Q;
Unionfind uf(N);
rep(i,N){
int c;cin>>c;uf.mp[i][c]=1;
}
rep(i,Q){
int q;cin>>q;
if(q==1){
int a,b;cin>>a>>b;a--;b--;
uf.unite(a,b);
}
else{
int x,y;cin>>x>>y;x--;
int r=uf.root(x);
cout<<uf.mp[r][y]<<endl;
}
}
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 998244353;
int n, m;
int f[3005][3005];
int main() {
cin >> n >> m;
f[1][1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = i; j; --j) {
f[i][j] = f[i - 1][j - 1];
if (j + j <= i) {
f[i][j] += f[i][j + j];
if (f[i][j] >= mod) f[i][j] -= mod;
}
}
}
cout << f[n][m];
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define itn int
#define rep2(i,a,n) for(int i = (a);i < (n);i++)
#define rep(i,n) rep2(i,0,n)
#define P pair<int,int>
#define endl '\n'
int dp[1001][1001];
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin >> n >> m;
int a[n],b[m];
rep(i,n)cin >> a[i];
rep(i,m)cin >> b[i];
rep(i,n+1){
rep(j,m+1){
dp[i][j] = 1e9;
}
}
dp[0][0] = 0;
rep(i,n+1){
rep(j,m+1){
if(i > 0)dp[i][j] = min(dp[i][j],dp[i-1][j] + 1);
if(j > 0)dp[i][j] = min(dp[i][j],dp[i][j-1] + 1);
if(i > 0 && j > 0){
if(a[i-1] == b[j-1])dp[i][j] = min(dp[i][j],dp[i-1][j-1]);
else dp[i][j] = min(dp[i][j],dp[i-1][j-1] + 1);
}
}
}
cout << dp[n][m] << endl;
} |
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
void read(int &x){
static char c;
while(!isdigit(c=getchar())); x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
void read(ll &x){
static char c;
while(!isdigit(c=getchar())); x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
void chkmax(int &x,int y){if(y>x)x=y;}
void chkmin(int &x,int y){if(y<x)x=y;}
void chkmax(ll &x,ll y){if(y>x)x=y;}
void chkmin(ll &x,ll y){if(y<x)x=y;}
const int N=5e3+10,Mod=998244353;
void Add(int &x,int y){(x+=y)>=Mod?x-=Mod:x;}
int Pow(int a,int b){
int res=1;
while(b){
if(b&1) res=1ll*res*a%Mod;
a=1ll*a*a%Mod,b>>=1;
}
return res;
}
int n,m;
int F[15][N],C[N][N];
int Calc(int dep,int k){
if(dep==0)
if((k&1)||k>n) return 0;
else return C[n][k];
if(~F[dep][k]) return F[dep][k];
int res=0,dig=(m>>(dep-1))&1;
for(int i=0;i<=k/2;++i) Add(res,1ll*C[n][2*i]*Calc(dep-1,2*(k-2*i)+dig)%Mod);
return F[dep][k]=res;
}
void Work(){
memset(F,-1,sizeof F);
read(n),read(m);
if(m==1) return puts("0"),void();
int Bit=log2(m);
C[0][0]=1;
for(int i=1;i<=n;++i) C[i][0]=1;
for(int i=1;i<=n;++i)
for(int j=1;j<=i;++j)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%Mod;
printf("%d\n",Calc(Bit-1,2+((m>>(Bit-1))&1)));
}
int main(){Work();}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#define int long long
const int MOD=998244353;
int dp[15][1000001];
int pw(int n,int k){
assert(k>=0);
int res=1;
while(k){
if(k&1)(res*=n)%=MOD;
(n*=n)%=MOD;
k>>=1;
}
return res;
}
int Factorial[5000000],Finverse[5000000];
inline void Cinit(){
Factorial[0]=1;
for(int i=1;i<5e6;i++)Factorial[i]=(Factorial[i-1]*i)%MOD;
Finverse[4999999]=pw(Factorial[4999999],MOD-2);
for(int i=4999998;i>=0;i--)Finverse[i]=(i+1)*Finverse[i+1]%MOD;
}
int memo[5001][5001];
int nCk(int n,int k){
if(n<k)return 0;if(k<0)return 0;
if(!Factorial[0])Cinit();
if(~memo[n][k])return memo[n][k];
int res=Factorial[n];
(res*=Finverse[k])%=MOD;
(res*=Finverse[n-k])%=MOD;
return memo[n][k]=res;
}
signed main(){
memset(memo,-1,sizeof(memo));
int n,m;cin>>n>>m;
dp[0][0]=1;
for(int i=0;i<14;i++){
for(int j=0;j<1000000;j++){
if(((m>>i)&1)^(j&1))continue;
if(dp[i][j]==0)continue;
int s=j/2;//上に繰り上がる予約
for(int k=0;2*k<=n;k++){//k新たに繰り上げる
//n個のうち2k個が1
(dp[i+1][s+k]+=dp[i][j]*nCk(n,2*k)%MOD)%=MOD;
}
}
}
cout<<dp[14][0]<<endl;
}
|
#pragma GCC optimize("Ofast")
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Po = pair<double, double>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=ll(a);i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define PB push_back
#define MP make_pair
#define SZ(x) ll(x.size())
#define ALL(x) x.begin(),x.end()
#define NIL -1
#define INF 1ll<<63-1
//#define MOD 1000000007
#define PI 3.14159265358979323846
#define endl "\n"
#define EPS 1e-9
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
template <class T, class U>
bool chmin(T& t, const U& u) {
if (t > u) {
t = u;
return true;
}
else return false;
}
template <class T, class U>
bool chmax(T& t, const U& u) {
if (t < u) {
t = u;
return true;
}
else return false;
}
bool YN(bool b) {
if (b) {
Yes;
return true;
}
else {
No;
return false;
}
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <unsigned int MOD>
struct ModInt {
using uint = unsigned int;
using ull = unsigned long long;
using M = ModInt;
uint v;
ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
M& set_norm(uint _v) { //[0, MOD * 2)->[0, MOD)
v = (_v < MOD) ? _v : _v - MOD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator+(const M& a) const { return M().set_norm(v + a.v); }
M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
M operator/(const M& a) const { return *this * a.inv(); }
M& operator+=(const M& a) { return *this = *this + a; }
M& operator-=(const M& a) { return *this = *this - a; }
M& operator*=(const M& a) { return *this = *this * a; }
M& operator/=(const M& a) { return *this = *this / a; }
M operator-() const { return M() - *this; }
M& operator++(int) { return *this = *this + 1; }
M& operator--(int) { return *this = *this - 1; }
M pow(ll n) const {
if (n < 0) return inv().pow(-n);
M x = *this, res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
M inv() const {
ll a = v, b = MOD, p = 1, q = 0, t;
while (b != 0) {
t = a / b;
swap(a -= t * b, b);
swap(p -= t * q, q);
}
return M(p);
}
bool operator==(const M& a) const { return v == a.v; }
bool operator!=(const M& a) const { return v != a.v; }
friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
static uint get_mod() { return MOD; }
};
using Mint = ModInt<998244353>;
signed main() {
cout << fixed << setprecision(15);
ll x; cin >> x;
if (x >= 0)cout << x << endl;
else cout << 0 << endl;
}
| #include<bits/stdc++.h>
#define N 200005
#define re register
#define ll long long
#define P 100007
using namespace std;
int n,m,K,q,u;
inline void Rd(int &res){
re char c;res=0;bool f=0;
while(c=getchar(),c<48)(c=='-')&&(f=1);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
f&&(res=-res);
}
int main(){
int T;Rd(T);
while(T--){
ll n;
scanf("%lld",&n);
int cnt=0;
while(!(n&1))cnt++,n>>=1;
if(cnt==0)puts("Odd");
else if(cnt==1)puts("Same");
else puts("Even");
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define MOD 1000000007
#define pb push_back
#define fi first
#define se second
#define endl "\n"
#define all(v) v.begin(), v.end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<VI> VVI;
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void yes(bool t = 1) { cout << yesno[t] << endl; }
const int mxN = 5e5+5;
int n,m,h;
int a[mxN];
string s,t;
array<int,2> b[mxN];
void code()
{
cin>>m>>h;
Yes(h%m==0);
/*
*/
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
while(t--) code();
} | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <iomanip>
#include <vector>
int main() {
int M, H;
cin >> M >> H;
if (H % M == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
|
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0 ,w = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-'){
w = -1;
c = getchar();
}
}
while(c >= '0' && c <= '9'){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * w;
}
inline int min(int a,int b){return a < b ? a : b;}
int a,b,x,y;
int p[205][205];
int main(){
a = read(),b = read(),x = read(),y = read();
memset(p,0x3f,sizeof p);
p[1][101] = p[101][1] = x;
for(int i = 2;i <= 100;i++){
p[i][i + 99] = p[i + 99][i] = p[i][i + 100] = p[i + 100][i] = x;
p[i][i - 1] = p[i - 1][i] = p[i + 100][i + 99] = p[i + 99][i + 100] = y;
}
b += 100;
for(int k = 1;k <= 200;k++)
for(int i = 1;i <= 200;i++)
for(int j = 1;j <= 200;j++){
if(i == j) continue;
p[i][j] = min(p[i][k] + p[k][j],p[i][j]);
}
printf("%d\n",min(p[a][b],p[b][a]));
return 0;
} | #pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dy[4]={ -1,0,1,0 };
const int dx[4]={ 0,-1,0,1 };
struct UnionFind{
private:
vector<int> par;
vector<int> siz;
public:
void init(int N){
par.resize(N);
siz.resize(N);
for(int i=0; i<N; i++) par[i] = i;
for(int i=0; i<N; i++) siz[i] = 1;
}
void unite(int a, int b){
int rootA = root(a);
int rootB = root(b);
if(rootA == rootB) return;
if(siz[rootA] < siz[rootB]) swap(rootA, rootB);
par[rootB] = rootA;
siz[rootA] += siz[rootB];
}
int root(int a){
if(par[a] == a) return a;
return par[a] = root(par[a]);
}
bool same(int a, int b){
return root(a) == root(b);
}
int size(int a){
return siz[root(a)];
}
};
template<long long Mod>
struct mint{
long long x;
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^(long long t) const {
mint ret(1);
mint a = *this;
while(t){
if(t&1){
ret *= a;
}
a *= a;
t >>= 1;
}
return ret;
}
mint inv() const { return *this ^ (Mod-2); }
mint& operator/=(const mint a) { return *this *= a.inv(); }
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint operator/(const mint a) const { return mint(*this) /= a; }
friend istream& operator>>(istream& is, mint& a) { return is >> a.x; }
friend ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
};
int N, K;
int A[50][50];
UnionFind r, c;
mint<MOD> fact(int i){
if(i == 0) return 1;
return fact(i-1) * i;
}
signed main(){
cin >> N >> K;
rep(i, N) rep(j, N) cin >> A[i][j];
r.init(N);
c.init(N);
rep(i, N){
rep(j, N){
bool ok = true;
rep(k, N){
ok &= (A[i][k] + A[j][k] <= K);
}
if(ok) r.unite(i, j);
ok = true;
rep(k, N){
ok &= (A[k][i] + A[k][j] <= K);
}
if(ok) c.unite(i, j);
}
}
mint<MOD> ans = 1;
rep(i, N){
if(c.root(i) == i){
ans *= fact(c.size(i));
}
if(r.root(i) == i){
ans *= fact(r.size(i));
}
}
cout << ans << endl;
} |
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) -> iterator of kth element (in sorted fashion : low - high)
// order_of_key(x) -> #numbers strictly less than x
#pragma GCC optimize("Ofast","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")
#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define fi first
#define se second
#define endl '\n'
using pii = pair<int , int>;
using ll = long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rng(0);
ll dp[10005][105], psm[10005][105], n, m;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
sync
int t = 1;
//cin >> t;
while(t--){
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != s.back()){
cout << 1 << endl;
}
else{
if (n <= 3){
cout << -1 << endl;
}
else{
bool f = 0;
for (int i = 1; i <= n - 3; i++){
f |= (s[i] != s[0] && s[i + 1] != s[0]);
}
cout << (f ? 2 : -1) << endl;
}
}
}
cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s ";
return 0;
} | #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <numeric>
//#include <bits/stdc++.h>
//#define rep(i,a,n) for (int i=a;i<n;i++)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
/*
テスト通りますように
●
/⌒ヽ
| |/⌒ヽ(ヽ
(` ∥ー⌒) |
| ̄|| ̄ ̄ ̄ ̄ ̄|
|―||―――――|
| U |
| ̄ ̄ ̄ ̄ ̄ ̄ ̄|
|_______|
|―――――|
|―――――|
wwWwwWwWWw
*/
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 200005;
const int MOD = 3;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
string s;
cin >> s;
int sz = s.size();
s += "#######";
int cnt = 0;
rep(i,sz){
if(s[i] == 'Z' && s[i+1] == 'O' && s[i+2] == 'N' && s[i+3] == 'e')
cnt++;
}
cout << cnt << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1010;
const int mod=1e9+7;
ll dp[maxn];
char x,y,z,f;
ll mypow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)ans=1ll*ans*a%mod;
a=1ll*a*a%mod;
b>>=1;
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
dp[0]=dp[1]=1;
cin>>n>>x>>y>>z>>f;
if(n==2){cout<<"1"<<endl;return 0;}
for(int i=2;i<=n+5;++i){
dp[i]=(dp[i-1]+dp[i-2])%mod;
}
ll ans=0;
if(y=='A'){
if(x=='A')ans=1;
else if(x=='B'&&z=='A')ans=dp[n-2];
else if(x=='B'&&z=='B')ans=mypow(2,n-3);
}else if(y=='B'){
if(f=='B')ans=1;
else if(f=='A'&&z=='B')ans=dp[n-2];
else if(f=='A'&&z=='A')ans=mypow(2,n-3);
}
cout<<ans;
return 0;
}
| #include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;
inline int read()
{
int x = 0,f = 1; char ch = getchar();
for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
return x*f;
}
const int mxN = 1000;
const int P = 1e9+7;
int n; char s[4];
llong pw2[mxN+3],f[mxN+3];
int main()
{
n = read(); for(int i=0; i<4; i++) {char str[3]; scanf("%s",str); s[i] = str[0]-65;}
if(n==2) {puts("1"); return 0;}
pw2[0] = 1ll; for(int i=1; i<=n; i++) pw2[i] = (pw2[i-1]<<1)%P;
f[1] = 1ll; for(int i=2; i<=n; i++) f[i] = (f[i-1]+f[i-2])%P;
if(s[1]==0)
{
if(s[0]==0) {puts("1");}
else
{
if(s[2]==0) {printf("%lld\n",f[n-1]);}
else {printf("%lld\n",pw2[n-3]);}
}
}
else
{
if(s[3]==1) {puts("1");}
else
{
if(s[2]==1) {printf("%lld\n",f[n-1]);}
else {printf("%lld\n",pw2[n-3]);}
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n; ll x; cin >> n >> x;
vector<ll> A(n); rep(i, n) cin >> A[i];
vector<ll> cnt(n);
{
ll rem = x;
rep(i, n) {
if(i == n-1) cnt[i] = rem / A[i];
else cnt[i] = (x % A[i+1]) / A[i];
}
}
vector<vector<ll>> dp(n, vector<ll>(2));
dp[0][0] = 1;
rep(i, n-1) {
if(cnt[i]) {
dp[i+1][0] += dp[i][0];
dp[i+1][1] += dp[i][0];
} else {
dp[i+1][0] += dp[i][0];
}
if(cnt[i]+1 < A[i+1]/A[i]) {
dp[i+1][0] += dp[i][1];
dp[i+1][1] += dp[i][1];
} else {
dp[i+1][1] += dp[i][1];
}
}
cout << dp[n-1][0] + dp[n-1][1] << endk;
return 0;
}
| #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template<typename T> bool chmax(T &a,T b) {if(a<b) {a=b; return true;} return false;}
template<typename T> bool chmin(T &a,T b) {if(a>b) {a=b; return true;} return false;}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>=1;i--)
#define all(vec) vec.begin(),vec.end()
#define fsort(vec) sort((vec).begin(),(vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tlll;
typedef tuple<int,int,int> tiii;
const ll mod=1e9+7;
const int inf=1<<30;
const ll lnf=1ll<<60;
int main(){
ll n; cin >> n;
vector<ll> a(n);
vector<ll> b(n);
vector<ll> c(n);
ll sum=0;
rep(i,n){
cin >> a[i] >> b[i];
c[i]=2*a[i]+b[i];
sum-=a[i];
}
rsort(c);
rep(i,n){
sum+=c[i];
if(sum>0){
//cout << sum << endl;
cout << i+1 << endl;
return 0;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) cout << x << endl
#define all(v) (v).begin(),(v).end()
#define YN(ok) print((ok ? "YES" : "NO"))
#define yn(ok) print((ok ? "Yes" : "No"))
#define decimal(n) setprecision(25) << n
#define Arrays_toString(v) rep(o,(v).size()){cout<<v[o]<<", ";if(o==(v).size()-1){cout<<endl;}}
vi primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int main(void){
int n; cin >> n;
vi v(n);
rep(i, n) cin >> v[i];
int m = primes.size();
ll res = 3e18;
rep(i, (1<<m)) {
ll s = 1;
rep(j, m) {
if (i & (1<<j)) {
s *= primes[j];
}
}
bool ok = true;
rep(j, n) {
ll g = __gcd(s, (ll)v[j]);
if (g == 1) ok = false;
}
if (ok) res = min(res, s);
}
print(res);
} | #include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;
inline int read()
{
int f=1,x=0;char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-1;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return f*x;
}
int main()
{
int a=read(),b=read(),c=read();
if(a>0&&b>0)
{
if(a==b) puts("=");
else if(a>b) puts(">");
else puts("<");
return 0;
}
if(c&1)
{
if(a<=0&&b<=0)
{
if(abs(a)==abs(b)) puts("=");
else if(abs(a)<abs(b)) puts(">");
else puts("<");
}
if(a<=0&&b>0) puts("<");
if(a>0&&b<=0) puts(">");
}
else
{
if(abs(a)==abs(b)) puts("=");
else if(abs(a)<abs(b)) puts("<");
else puts(">");
}
return 0;
} |
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>
using namespace std;
//using namespace atcoder;
#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << " " << p.second << ")";
}
template<class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;
// edit
void solve() {
string S;
cin >> S;
string T = "ZONe";
int cnt = 0;
for (int i = 0; i + T.size() <= S.size(); ++i) {
cerr << S.substr(i, 4) << endl;
if (S.substr(i, 4) == T) {
cnt++;
}
}
cout << cnt << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
solve();
return 0;
}
| #include<iostream>
#include <algorithm>
using namespace std;
int main()
{
string S;
cin >> S;
int result = 0;
for (int i = 0; i < 9; i++)
{
if (S[i] == 'Z' && S[i+1]=='O' && S[i+2]=='N' && S[i+3]=='e')
{
result++;
}
}
cout << result;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int x,y;
cin >> x >> y;
if(x == y){
cout << x;
}else{
if(x!=1 && y!=1){
cout << 1;
}else if(x!=2 && y!=2){
cout << 2;
}else{
cout << 0;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ip cin>>
#define op cout<<
#define int long long
signed main(){
fast
int a, b, c;
ip a>>b;
if(a == b) {
c = a;
} else if(a == 1 && b == 2){
c = 0;
} else if(a == 2 && b == 1){
c = 0;
} else if(a == 0 && b == 1 || a == 1 && b ==0) {
c = 2;
} else if(a == 0 && b == 2 || a == 2 && b == 0) {
c = 1;
}
op c<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define Rep(i,n) for(int i=0;i<=(int)n;i++)
const ll mod = 1e9+7 ;
const ll INF = 1e18 ;
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
int main(){
ll n;
cin >> n;
vector<double> le(n);
vector<double> ri(n);
rep(i,n) {
int t;
double l, r;
cin >> t >> l >> r;
if(t == 2) r -= 0.01;
if(t == 3) l += 0.01;
if(t == 4) {
l += 0.01;r -= 0.01;
}
le.at(i) = l;
ri.at(i) = r;
}
ll ans = 0;
rep(i,n) {
for(int j = i + 1; j < n; j++) {
if(le.at(i) <= le.at(j)) {
if(ri.at(i) < le.at(j)) continue;
else ans++;
}
else {
if(ri.at(j) < le.at(i)) continue;
else ans++;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, count = 0;
cin >> N;
vector<vector<long long>> A(N, vector<long long>(3));
for (int i = 0; i < N; i++) {
for (int j = 0; j < 3; j++) {
cin >> A.at(i).at(j);
}
}
for (int i = 0; i < (N - 1); i++) {
for (int j = i; j < (N - 1); j++) {
if (A.at(i).at(2) > A.at(j + 1).at(1)) {
if (A.at(i).at(1) < A.at(j + 1).at(2)) {
count++;
}
else if (A.at(i).at(1) == A.at(j + 1).at(2)) {
if (A.at(i).at(0) == 1 || A.at(i).at(0) == 2) {
if (A.at(j + 1).at(0) == 1 || A.at(j + 1).at(0) == 3) {
count++;
}
}
}
}
else if (A.at(i).at(2) == A.at(j + 1).at(1)) {
if (A.at(i).at(0) == 1 || A.at(i).at(0) == 3) {
if (A.at(j + 1).at(0) == 1 || A.at(j + 1).at(0) == 2) {
count++;
}
}
}
}
}
cout << count << endl;
} |
// Fearless
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair <int, int>
#define pll pair <long long, long long>
#define PI (2.0 * acos(0.0))
#define testcase ll T; cin >> T; for (ll tc = 1; tc <= T; tc++)
#define info pair<int, pii>
#define mkp make_pair
const long long M = 1e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
int dis[2002][2002], vis[2002][2002], cvis[30];
vector < pii > adj[2002][2002];
vector < pii > pos[30];
int fx[4] = {0, 1};
int fy[4] = {1, 0};
void solve () {
int r, c;
pii src, tr;
cin >> r >> c;
char str[r + 2][c + 2];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> str[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (str[i][j] == '#') continue;
if (str[i][j] >= 'a' && str[i][j] <= 'z') {
pos[str[i][j] - 'a'].pb(pii(i, j));
}
if (str[i][j] == 'S') {
src = pii(i, j);
}
if (str[i][j] == 'G') {
tr = pii(i, j);
}
for (int k = 0; k < 2; k++) {
if (i + fx[k] < 0 || i + fx[k] >= r || j + fy[k] < 0 || j + fy[k] >= c) continue;
if (str[i + fx[k]][j + fy[k]] == '#') continue;
pii p1 = pii(i, j), p2 = pii(i + fx[k], j + fy[k]);
adj[i][j].pb(p2);
adj[i + fx[k]][j + fy[k]].pb(p1);
}
}
}
queue < pii > q;
q.push(src);
while (!q.empty()) {
pii u = q.front();
q.pop();
int ux = u.first, uy = u.second;
if (str[ux][uy] >= 'a' && str[ux][uy] <= 'z' && cvis[str[ux][uy] - 'a'] == 0) {
cvis[str[ux][uy] - 'a'] = 1;
for (pii v : pos[str[ux][uy] - 'a']) {
int vx = v.first, vy = v.second;
//cout << vx << " this " << vy << endl;
if (vis[vx][vy]) continue;
dis[vx][vy] = dis[ux][uy] + 1;
vis[vx][vy] = 1;
q.push(v);
}
}
//cout << adj[ux][uy].size() << " size " << endl;
for (pii v : adj[ux][uy]) {
int vx = v.first, vy = v.second;
//cout << vx << " " << vy << " now " << vis[vx][vy] << endl;
if (vis[vx][vy]) continue;
dis[vx][vy] = dis[ux][uy] + 1;
vis[vx][vy] = 1;
q.push(v);
}
}
if (!vis[tr.first][tr.second]) cout << -1 << endl;
else cout << dis[tr.first][tr.second] << endl;
}
int main () {
//freopen("E:/newts/in.txt", "r", stdin);
//freopen("E:/newts/out.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//testcase {
solve ();
//}
return 0;
}
|
/**
* Coded by : lucky_21
* --------Lokesh Singh
**/
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)
typedef double db;
typedef long long ll;
const int N=2005;
const int mod=1e9+7;
int h,w,dis[N][N];
vector<pii>v[26];
char g[N][N];
bool inside(int x,int y){
if(g[x][y]=='#') return 0;
if(x>0 and y>0 and x<=h and y<=w) return 1;
return 0;
}
int bfs(pii p){
queue<pii>q;
q.push(p);
rep(i,1,h) rep(j,1,w) dis[i][j]=1e9;
dis[p.F][p.S]=0;
while(!q.empty()){
p=q.front();
q.pop();
if(g[p.F][p.S]=='G') return dis[p.F][p.S];
if(islower(g[p.F][p.S])){
for(pii t:v[g[p.F][p.S]-'a']) if(t!=p and dis[t.F][t.S]>dis[p.F][p.S]+1){
q.push(t);
dis[t.F][t.S]=dis[p.F][p.S]+1;
}
v[g[p.F][p.S]-'a'].clear();
}
rep(i,-1,1) rep(j,-1,1) if(abs(i)+abs(j)<=1 and dis[p.F+i][p.S+j]>dis[p.F][p.S]+1 and inside(p.F+i,p.S+j)){
dis[p.F+i][p.S+j]=dis[p.F][p.S]+1;
q.push({p.F+i,p.S+j});
}
}
return -1;
}
signed main(){
FastIO;
cin>>h>>w;
pii st;
rep(i,1,h) rep(j,1,w){
cin>>g[i][j];
if(islower(g[i][j])) v[g[i][j]-'a'].pb({i,j});
if(g[i][j]=='S') st={i,j};
}
cout<<bfs(st);
return 0;
}
|
#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 pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
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 pair<T, T>& v) {
o << "(" << v.first << "," << v.second << ")";
return o;
}
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);
/*
どの頂点を選ぶか?
その頂点を選ぶことによって、どれだけ自分がプラスになるか
自分の手番が継続できるか
優先度
手番が継続できて、収支プラス
↓
手番が継続できなくて、収支プラス(大きい方から)
↓
手番が継続できなくて、収支マイナス(マシな方から)
↓
手番が継続できて、収支マイナス
*/
int N; cin >> N;
vvi es(N);
FOR(i, 1, N) {
int p; cin >> p; p--;
es[p].push_back(i);
}
v(bool) bs(N); // 手番が継続できるかどうか
vi ss(N); // 収支
int big = 2000002;
auto vscore = [&](int v) {
int s = ss[v];
if (bs[v]) {
if (ss[v] >= 0) s += big;
else s -= big;
}
return s;
};
auto vcmp = [&](int a, int b) {
return vscore(a) < vscore(b);
};
auto dfs = [&](auto dfs, int v) {
if (!sz(es[v])) {
bs[v] = false; // 相手の手番に
ss[v] = -1;
return;
}
priority_queue<int, deque<int>, decltype(vcmp)> pq(vcmp);
for (auto c : es[v]) {
dfs(dfs, c);
pq.push(c);
}
int turn = 1; // 相手の手番なら-1
int score = 0;
while (sz(pq)) {
auto c = pq.top(); pq.pop();
score += ss[c] * turn;
if (!bs[c]) turn = turn * -1;
}
ss[v] = -1+score;
bs[v] = turn < 0; // 1回戻るので、手番は逆になる
};
dfs(dfs, 0);
//cout << ss;
/*
a+b=N a-b=ss[0]
*/
int a = (N - ss[0]) / 2;
cout << a << newl;
}
#endif
| #include <bits/stdc++.h>
#define int long long
#define db(a) cout << a << endl
#define db2(a,b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a,b) cout << #a << ": " << a << ", " << #b << ": " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a) for(auto _i:a) cout << _i << " "; cout << endl
#define adbp(a) for(auto _i:a) cout << _i.first << " " << _i.second << endl;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
#define N 3010
#define M 200010
using namespace std;
int n;
vector<int> a, pre;
vector<vector<int>> dp;
vector<vector<vector<int>>> modmap;
// i: current position, j: current subset
int solve(int i, int j)
{
if(i == n) return 1;
if(dp[i][j] != -1) return dp[i][j];
int curmod = pre[i] % j;
auto &mp = modmap[j][curmod];
auto index = lower_bound(mp.begin(), mp.end(), i) - mp.begin();
int ret = (index == mp.size()) ? (0) :
(solve(mp[index] + 1, j) + (mp[index] != n - 1) * solve(mp[index] + 1, j + 1));
ret = (ret % mod);
return dp[i][j] = ret;
}
signed main()
{
fastIO;
cin >> n;
a.resize(n);
for(int i=0; i<n; i++) cin >> a[i];
pre.resize(n + 1);
for(int i=0; i<n; i++) pre[i + 1] = pre[i] + a[i];
modmap.assign(n + 1, vector<vector<int>>(n + 1, vector<int>()));
for(int i=1; i<=n; i++)
{
for(int j=0; j<n; j++)
{
modmap[i][pre[j + 1] % i].push_back(j);
}
}
dp.assign(n + 1, vector<int>(n + 1, -1));
cout << solve(0, 1);
} |
#include <iostream>
using namespace std;
int main(void){
int x, y;
cin >> x >> y;
if (x == y) {
cout << x << endl;
} else if (x == 0 && y == 1) {
cout << 2 << endl;
} else if (x == 1 && y == 0) {
cout << 2 << endl;
} else if (x == 0 && y == 2) {
cout << 1 << endl;
} else if (x == 2 && y == 0) {
cout << 1 << endl;
} else if (x == 1 && y == 2) {
cout << 0 << endl;
} else if (x == 2 && y == 1) {
cout << 0 << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, W;
cin >> N >> W;
int ans = N / W;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int nax=2e5+100,mod=1e9+7;
int dp[nax][19];
int32_t main()
{
int t=1;
//cin>>t;
while(t--)
{
string a;
cin>>a;
int k;
cin>>k;
set<int> s;
s.clear();
for(int i=0;i<a.size();i++)
{
for(int j=0;j<17;j++)
{
dp[i+1][j]+=dp[i][j]*j;
dp[i+1][j]%=mod;
dp[i+1][j+1]+=dp[i][j]*(16-j);
dp[i+1][j+1]%=mod;
}
int num=a[i]-'0';
if(a[i]>='A')
num=10+a[i]-'A';
for(int j=0;j<num;j++)
{
if(i==0 and j==0)
continue;
if(s.count(j))
{
dp[i+1][s.size()]++;
dp[i+1][s.size()]%=mod;
}
else
{
dp[i+1][s.size()+1]++;
dp[i+1][s.size()+1]%=mod;
}
}
s.insert(num);
if(i!=0)
dp[i+1][1]+=15;
}
int res=dp[a.size()][k];
set<char> ss;
for(int i=0;i<a.size();i++)
ss.insert(a[i]);
if(ss.size()==k)
++res;
cout<<res;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,a,n) for (ll i = a;i<(n);++i)
#define all(v) v.begin(),v.end()
#define bit(n) (1LL<<(n))
#define pu(n) push_back(n)
#define INF 1000000000000 //10^12
#define MOD 10000007 //10^9+7
#define MAXR 100000 //10^5
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; }
class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};
bool IsPrime(ll num){
if (num < 2) return false;else if (num == 2) return true;else if (num % 2 == 0) return false; double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2){
if (num % i == 0)return false;
}
return true;
}//???true
ll gcd(ll a, ll b){
if (b == 0)return a;
return gcd(b, a % b);
}//?????
ll lcm(ll a, ll b){
return (a / gcd(a, b)) * b;
}//?????
uintmax_t combi(unsigned int n, unsigned int r) {
if ( r * 2 > n ) r = n - r;uintmax_t dividend = 1;uintmax_t divisor = 1;for ( unsigned int i = 1; i <= r; ++i ) { dividend *= (n-i+1);divisor *= i;}return dividend / divisor;
}
using Graph = vector<vector<int>>;
/*--------------<<fixed<<setprecision(15)----->>-------------------------------------------------------------------------------------------------------------*/
int main(){
ll n,m;cin>>n>>m;
vector<pair<ll,ll>>ab(m);
rep(i,0,m){ll a,b;cin>>a>>b;ab[i].first=a-1;ab[i].second=b-1;}
ll k;cin>>k;
vector<pair<ll,ll>>cd(k);
rep(i,0,k){ll a,b;cin>>a>>b;cd[i].first=a-1;cd[i].second=b-1;}
ll ans=0;
rep(bit,0,(1<<k)){
vector<ll>dish(n,0);ll cnt=0;
rep(i,0,k){
if(bit&(1<<i)){
dish[cd[i].second]++;
}
else{
dish[cd[i].first]++;
}
}
rep(i,0,m){
bool c=true;
if(!dish[ab[i].first]>=1)c=false;
if(!dish[ab[i].second]>=1)c=false;
if(c)cnt++;
}
ans=max(ans,cnt);
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=200005,INF=1<<30;
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
string S;cin>>S;
deque<char> ans;
bool f=false;
for(char c:S){
if(c=='R'){
f^=1;
}else{
if(!f){
ans.push_back(c);
if(si(ans)>=2&&ans[si(ans)-1]==ans[si(ans)-2]){
ans.pop_back();
ans.pop_back();
}
}else{
ans.push_front(c);
if(si(ans)>=2&&ans[0]==ans[1]){
ans.pop_front();
ans.pop_front();
}
}
}
}
if(f){
for(int i=si(ans)-1;i>=0;i--) cout<<ans[i];
cout<<endl;
}else{
for(int i=0;i<si(ans);i++) cout<<ans[i];
cout<<endl;
}
}
| #pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define ceil(_,__) (_+(__-1))/__
#define INF 0x3f3f3f3f
#define ll_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
inline int read()
{
int x=0,y=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') y=-1;c=getchar();}
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*y;
}
deque<char> dq;
int main(){
bool fl=false;
char ch;
while(cin>>ch)
if(ch=='R') fl^=1;
else if(fl) dq.pf(ch);
else if(!fl) dq.pb(ch);
string res;
while(dq.size()){
if(res.size() && res.back()==dq.front()) res.pop_back();
else res+=dq.front();
dq.pop_front();
}
if(fl) reverse(res.begin(), res.end());
cout<<res<<endl;
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(){
cin.tie(0);
ios::sync_with_stdio(0);
ll K,N,M; cin >> K >> N >> M;
vector<ll> A(K),B(K);
rep(i,K) cin >> A[i];
rep(i,K) B[i] = M * A[i] / N;
ll x = M;
vector<pair<ll,ll>> v;
rep(i,K) x -= B[i];
rep(i,K) v.push_back({M * A[i] - N * B[i], i});
sort(v.rbegin(), v.rend());
rep(i,x) B[v[i].second] += 1;
rep(i,K){
if(i) cout << " ";
cout << B[i];
}cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
#define TO_STRING(VariableName) # VariableName
#define debug(x) cerr<<TO_STRING(x)<<" : "<<x<<" "<<endl;
void debugs() { cerr << endl; }
template <class T, class... Args>
void debugs(const T& x, const Args &... args) {
cerr << x << " ";
debugs(args...);
}
template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
ost << "{ " << p.first << ", " << p.second << " }";
return ost;
}
template<class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
ost << "{ ";
for (int i = 0; i < v.size(); i++) {
if (i)ost << ", ";
ost << v[i];
}
ost << " }";
return ost;
}
template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>& v) {
ost << "{ ";
for (auto p : v) {
ost << "{ " << p.first << ", " << p.second << " }";
}
ost << " }";
return ost;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
ll u[212345];
int main() {
ll n, w;
cin >> n >> w;
ll mt = 0;
rep(i,n){
ll s, t, p;
cin >> s >> t >> p;
//s--;
//t--;
u[s]+=p;
u[t]-=p;
mt = max(mt,t);
}
rep(i,mt-1) u[i+1] += u[i];
bool f = false;
rep(i,mt) if (u[i] > w) f=true;
(f) ? cout << "No" << endl : cout << "Yes" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
/*
提出前に確認!
- 配列は+10ぐらい余分にとっておく
- setとmultisetを間違えない
*/
/*
デバッグ時にチェック!
- RE
配列の取りうる最大値、最小値を**すべて**チェックする
*/
int a[110];
int b[110];
bool used[220];
bool dp[200][200];
int zero_cnt[200][200];
int main(void)
{
int n;
cin >> n;
int zero_all = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
if (a[i] == -1 && b[i] == -1) zero_all++;
if (a[i] != -1 && b[i] != -1 && a[i] > b[i])
{
cout << "No" << endl;
return (0);
}
if (a[i] != -1)
{
if (used[a[i]])
{
cout << "No" << endl;
return (0);
}
used[a[i]] = true;
}
if (b[i] != -1)
{
if (used[b[i]])
{
cout << "No" << endl;
return (0);
}
used[b[i]] = true;
}
}
dp[0][1] = true;
for (int i = 1; i <= n + 1; i++)
{
for (int j = i + 1; j <= n + 1; j++)
{
bool is_valid = true;
for (int k = 0; k < j - i; k++)
{
bool flag = false;
for (int w = 0; w < n; w++)
{
if (flag) break ;
if (a[w] == i * 2 - 1 + k && b[w] == i * 2 - 1 + (j - i) + k) flag = true;
else if (a[w] == -1 && !used[i * 2 - 1 + k] && b[w] == i * 2 - 1 + (j - i) + k) flag = true;
else if (a[w] == i * 2 - 1 + k && b[w] == -1 && !used[i * 2 - 1 + (j - i) + k]) flag = true;
else if (a[w] == -1 && !used[i * 2 - 1 + k] && b[w] == -1 && !used[i * 2 - 1 + (j - i) + k])
{
flag = true;
zero_cnt[i][j]++;
}
}
if (!flag) is_valid = false;
}
dp[i][j] = is_valid;
}
}
for (int i = 0; i <= n + 1; i++)
{
for (int j = i + 1; j <= n + 1; j++)
{
for (int v = j + 1; v <= n + 1; v++)
{
if (dp[i][j] && dp[j][v] && zero_cnt[i][j] + zero_cnt[j][v] <= zero_all) dp[i][v] = true, zero_cnt[i][v] = zero_cnt[i][j] + zero_cnt[j][v];
}
}
}
if (dp[0][n + 1] && zero_cnt[0][n + 1] <= zero_all) cout << "Yes" << endl;
else cout << "No" << endl;
return (0);
}
| #include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
// #define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 3000+10;
int a[maxn][6];
int n;
int v[maxn];
bool present[50];
bool help(int x){
int temp = 31;
for (int i = 0; i < (1 << 5); ++i)
{
if(!present[i]) continue;
if((i|x) == temp) return 1;
}
return 0;
}
bool check(int x){
fo(i, 1, n){
v[i] = 0;
for (int j = 0; j < 5; ++j)
{
if(a[i][j] >= x) v[i] |= (1 << j);
}
}
for (int i = 0; i < 40; ++i)
{
present[i] = 0;
}
fo(i, 1, n){
if(help(v[i])) return 1;
fo(j, 1, i - 1){
present[(v[i] | v[j])] = 1;
}
}
return 0;
}
int32_t main(){
fast_io;
cin >> n;
fo(i, 1, n){
for (int j = 0; j < 5; ++j)
{
cin >> a[i][j];
}
}
int ans = 1, l = 1, r = 1e9;
while(l <= r){
int m = (l + r) / 2;
if(check(m)){
ans = m;
l = m + 1;
}
else{
r = m - 1;
}
}
show(ans);
return 0;
} |
/*input
*/
#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define bits(x,i) (((x) >> (i)) & 1)
#define mid ((l+r)/2)
#define pr pair<int,int>
#define ll long long
using namespace std;
const int N = 2005;
int n,m;
int a[N];
const int mod = 1e9 + 7;
ll power(ll x, ll n){
ll ans = 1;
for(int i=0;i<31;++i){
if (bits(n, i)) ans = (ans * x) % mod;
x = (x * x) % mod;
}
return ans;
}
ll inv(ll num, ll den){
return (num * power(den, mod - 2)) % mod;
}
int s = 0;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
up(i,1,n) cin >> a[i], s += a[i];
ll ans = 1;
if (m < s) { cout << "0"; return 0; }
up(i, m - s + 1, m + n) ans = (ans * i) % mod;
up(i,2,s + n) ans = inv(ans, i);
cout << ans;
} | #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL mod = 1e9 + 7;
const int MAXN = 4e6 + 5 + 2e3;
LL inv[MAXN];
LL C(LL a , LL b) {
LL res = 1;
for (LL i = 1; i <= b; ++i) {
res = res * (a - i + 1) % mod * inv[i] % mod;
}
return res;
}
int main() {
inv[1] = 1;
for (int i = 2; i <= MAXN - 5; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
LL n , m;
scanf("%lld %lld" , &n , &m);
LL tot = 0;
for (int i = 1; i <= n; ++i) {
int x;scanf("%d" , &x);
tot += x;
}
printf("%lld" , C(m + n , tot + n));
return 0;
} |
// @author: the__martian
// 108
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
#define trav(a, x) for (auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int h, w; cin >> h >> w;
int mx = 500000;
vector<vector<int>> v(h, vector<int>(w));
for (auto &r : v) {
for (auto &e : r) {
cin >> e;
mx = min(e, mx);
}
}
ll res = 0;
for (auto &r : v) {
for (auto &e : r) {
res += e - mx;
}
}
cout << res;
} |
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
x = (x > y ? y : x);
}
void solve() {
using D = long double;
int n;cin >> n;
V<int>a (n);
forn(i, n) cin >> a[i];
D ans = 0;
sort(all(a));
D x = a[(n - 1) / 2] * 0.5;
ans += n * x;
forn(i, n) {
ans -= min((D)a[i], x * 2.0);
ans += a[i];
}
ans /= n;
cout << FIXED_FLOAT(ans) << '\n';
}
void test_case() {
int t;
cin >> t;
forn(p, t) {
//cout << "Case #" << p + 1 << ": ";
solve();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
#define N 1010
vector<vector<ll>> ansl(N,vector<ll>(N,INF));
vector<vector<vector<ll>>> conn(N,vector<vector<ll>>(26));
int main(){
ll n, m; cin >> n >> m;
REP(i,m) {
ll a, b; char c; cin >> a >> b >> c;
a--; b--; c-='a';
conn[a][c].pb(b); conn[b][c].pb(a);
}
ansl[0][n-1]=0;
queue<P> q;
q.push({0,n-1});
while (!q.empty()) {
ll x = q.front().first; ll y = q.front().second; q.pop();
REP(i,26) {
for (ll nex : conn[x][i]) for (ll ney : conn[y][i]) if (ansl[nex][ney]==INF) {
ansl[nex][ney]=ansl[x][y]+1;
q.push({nex,ney});
}
}
}
ll ans = INF;
REP(i,n) ans = min(ans,2*ansl[i][i]);
REP(i,n) {
REP(j,26) for (ll k : conn[i][j]) {
ans = min(ans,2*ansl[i][k]+1);
}
}
if (ans==INF) cout << -1 << endl;
else cout << ans << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int n, m;
vector<pair<int, char>> edges[1000];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; ++i)
edges[i].clear();
for (int i = 0; i < m; ++i) {
int from, to;
char c;
cin >> from >> to >> c;
--from;
--to;
edges[from].push_back({ to, c });
edges[to].push_back({ from, c });
};
queue<pair<int, int>> q, qq;
q.push(make_pair(0, n - 1));
int d = 0;
int res = -1;
bool visited[1000][1000] = { 0 };
while (!q.empty()) {
while (!q.empty()) {
auto front = q.front();
q.pop();
int x = front.first;
int y = front.second;
for (auto edgeA : edges[x]) {
for (auto edgeB : edges[y]) {
if (edgeA.second != edgeB.second)
continue;
if (visited[edgeA.first][edgeB.first])
continue;
visited[edgeA.first][edgeB.first] = true;
if (edgeA.first == edgeB.first) {
res = (res == -1 || res > 2 * (d + 1)) ? 2 * (d + 1) : res;
} else if (edgeA.first == y && edgeB.first == x) {
res = (res == -1 || res > 2 * d + 1) ? 2 * d + 1 : res;
} else {
qq.push(make_pair(edgeA.first, edgeB.first));
}
}
}
}
swap(q, qq);
++d;
}
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000000],tree[1000000];
void update(ll i,ll val,ll s,ll e,ll idx)
{
if(i==s&&s==e)
{
tree[idx]=tree[idx]^val;
return;
}
if(i>e||i<s)
{
return;
}
ll mid=(s+e)/2;
update(i,val,s,mid,2*idx);
update(i,val,mid+1,e,2*idx+1);
tree[idx]=tree[2*idx]^tree[2*idx+1];
}
ll query(ll qs,ll qe,ll s,ll e,ll idx,ll ee)
{
if(s>=qs&&e<=qe)
return tree[idx];
if(e<qs||s>qe)
return 0;
ll mid=(s+e)/2;
ll left = query(qs,qe,s,mid,2*idx,ee);
ll right = query(qs,qe,mid+1,e,2*idx+1,ee);
return left^right;
}
void build(ll idx,ll s,ll e)
{
if(s==e)
{
tree[idx]=a[s];
return;
}
if(s>e)
return;
ll mid = (s+e)/2;
build(2*idx,s,mid);
build(2*idx+1,mid+1,e);
tree[idx]=tree[2*idx]^tree[2*idx+1];
//cout<<s<<' '<<e<<':'<<tree[idx]<<'\n';
}
void fun()
{
ll n,q;
cin>>n>>q;
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
build(1,0,n-1);
ll q1,q2,qa;
while(q--)
{
cin>>qa>>q1>>q2;
q1--;
q2--;
if(qa==2)
cout<<query(q1,q2,0,n-1,1,q1)<<'\n';
else
update(q1,q2+1,0,n-1,1);
}
}
int main() {
ll t;
//cin>>t;
t=1;
while(t--)
fun();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct BIT{
vector<int> a;
BIT(int n):a(n+1){}
void xadd(int i,int x){
while(i<a.size()){
a[i]^=x;
i+=i&-i;
}
}
int xsum(int i){
int ret=0;
while(i>0){
ret^=a[i];
i-=i&-i;
}
return ret;
}
};
int main(){
int n,q;
cin>>n>>q;
BIT bit(n);
for(int i=1;i<=n;i++){
int a;
cin>>a;
bit.xadd(i,a);
}
for(int i=0;i<q;i++){
int t,x,y;
cin>>t>>x>>y;
if(t==1)
bit.xadd(x,y);
else
cout<<(bit.xsum(y)^bit.xsum(x-1))<<endl;
}
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main(){
int N ; cin >> N;
ll T; cin >> T;
vector<ll> A(N); rep(i,N) cin >> A[i];
int n1=N/2, n2=N-N/2;
vector<ll> v1,v2;
rep(i,(1<<n1)){
ll sum = 0;
rep(j,n1){
if(i&(1<<j)) sum+=A[j];
}
v1.pb(sum);
}
rep(i,(1<<n2)){
ll sum = 0;
rep(j,n2){
if(i&(1<<j)) sum+=A[j+N/2];
}
v2.pb(sum);
}
sort(all(v1));
sort(all(v2));
ll ans = 0;
rep(i,sz(v1)){
ll tmp = v1[i];
if(tmp>T) continue;
if(upper_bound(all(v2),T-tmp)!=v2.begin()){
tmp += *(upper_bound(all(v2),T-tmp)-1);
}
chmax(ans, tmp);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=a;(i)<(b);++(i))
#define RFOR(i,a,b) for(ll i=a;(i)>=(b);--(i))
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,n,0)
#define ALL(v) v.begin(), v.end()
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)), v.end())
#define BIT(n) (1LL<<(n))
#define DEBUG(a) cerr << #a << " = " << a << endl
const double PI = acos(-1);
const int inf = 1001001001;
const int mod = (int)1e9+7;
//const ll inf = 1e15;
//const ll mod = 1e9+7;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int mex(vector<int> a) {
sort(ALL(a));
if (a[0] - 1 >= 0) return a[0] - 1;
for(int i = 1; i < a.size(); i++) {
if (a[i] - a[i-1] > 1) return a[i-1] + 1;
}
return a[a.size()-1] + 1;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n);
REP(i, n) cin >> a[i];
//vector<int> am(m);
//for(int i = 0; i < m; i++) {
// am[i] = a[i];
//}
//int mex_n = mex(am);
//for(int i = m; i < n; i++) {
// am.erase(am.begin());
// am.push_back(a[i]);
// mex_n = min(mex_n, mex(am));
//}
//cout << mex_n << endl;
vector<int> ans;
vector<int> pre(n, -1);
REP(i, n) {
if (i - pre[a[i]] > m) {
ans.push_back(a[i]);
}
pre[a[i]] = i;
}
REP(i, n) {
if (n - pre[i] > m) {
ans.push_back(i);
}
}
if (ans.size() == 0) {
cout << n << endl;
return 0;
}
sort(ALL(ans));
cout << ans[0] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1e9
#define linf 1e18
#define BASE 1000000
#define EPS 1e-10
#define PI acos(-1)
#define pii pair<int,int>
#define piii pair<int, pair<int, int> >
#define pdi pair<double,int>
#define vi vector<int>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define ms(x,val_add) memset(x, val_add, sizeof(x))
#define pb(x) push_back(x)
#define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ;
#define dbg(x) do { cout << #x << " = " << x << endl; } while(0)
#define mp(x, y) make_pair(x, y)
#define minimize(x, y) x = min(x, y)
#define maximize(x, y) x = max(x, y)
/*** IMPLEMENTATION ***/
bool exitInput = false;
int ntest = 1, itest = 1 ;
const int dx[4] =
{
-1, 0, 1, 0
};
const int dy[4] =
{
0, 1, 0, -1
};
// const int dx[8] = {-2, -1, -1, +0, +0, +1, +1, +2};
// const int dy[8] = {+0, -1, +1, -2, +2, -1, +1, +0};
/** Knight Move **/
// const int dx[8] = {+1, +2, +2, +1, -1, -2, -2, -1};
// const int dy[8] = {+2, +1, -1, -2, -2, -1, +1, +2};
const ll Mod = 1000000007LL;
const int maxn = 500 + 5;
const int maxv = 500 + 5;
const int maxe = 10000 + 5;
ll addMod( ll &x, ll y )
{
x = ( x + y ) % Mod;
}
int main()
{
#ifdef HOME
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );
#endif // HOME
ios_base::sync_with_stdio( false );
cin.tie( NULL );
cout << setprecision( 10 );
cout << fixed;
int A, B;
cin >> A >> B;
A = A + B;
if(A >= 15 && B >= 8)
cout << 1;
else if(A >= 10 && B >= 3)
cout << 2;
else if(A >= 3)
cout << 3;
else
cout << 4;
return 0;
}
| #include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// @@ !! LIM()
int main(/* int argc, char *argv[] */) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << setprecision(20);
ll A, B; cin >> A >> B;
ll ans = 1;
for (ll x = 1; x <= B; x++) {
ll z = (B / x) * x;
ll y = z - x;
if (A <= y) ans = x;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define pb push_back
#define ss second
#define ff first
#define all(v) v.begin(), v.end()
#define deb(x) cerr << "\n" \
<< #x << "=" << x << "\n";
#define deb2(x, y) cerr << "\n" \
<< #x << "=" << x << "\n" \
<< #y << "=" << y << "\n";
#define w(x) \
int x; \
cin >> x; \
while (x--)
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
pair<int,int> t;
cin >> t.ff >> t.ss;
double ans = 0.0;
pair<int,int> v[n];
double x2 = t.ff,y2 = t.ss;
for(int i = 0; i < n; ++i) {
cin >> v[i].ff >> v[i].ss;
double x1 = v[i].ff, y1 = v[i].ss;
if(x1 == x2)
continue;
double cur = (y1 * x2 - x1 * y2) / (x2 - x1);
ans = max(ans,cur);
}
cout <<fixed << setprecision(6) << ans;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9+7;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> needs(n, vector<int>(4));
vector<int> a(n), b(n), c(n), d(n);
rep(i,n) {
int x, y, r;
cin >> x >> y >> r;
needs[i][0] = r; needs[i][1] = x; needs[i][2] = y; needs[i][3] = i;
a[i] = x, c[i] = x+1;
b[i] = y, d[i] = y+1;
}
sort(needs.begin(), needs.end());
// rep(i,n) cout << needs[i][0] << ' ' << needs[i][1] << ' ' << needs[i][2] << ' ' << needs[i][3] << ' ' << "\n";
rep(i,n) cout << a[i] << ' ' << b[i] << ' ' << c[i] << ' ' << d[i] << ' ' << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> neigh[105];
bool vis[105];
int cnt[105];
void dfs(int v)
{
if (vis[v]) return;
vis[v] = true;
cnt[v]++;
for (int u : neigh[v]) {
dfs(u);
}
}
long double fact[105];
long double C(int n, int m) {
if (m > n) return 0;
return fact[n] / fact[m] / fact[n - m];
}
long double f(int n, int m)
{
long double res = 0;
for (int i = 1; i <= n; i++) {
res += C(n - i, m) * fact[m] * fact[n - m - 1];
}
res /= fact[n];
return res;
}
int main()
{
fact[0] = 1;
for (int i = 1; i < 105; i++) {
fact[i] = fact[i - 1] * i;
}
int n;
ignore = scanf("%d", &n);
for (int i = 0; i < n; i++) {
char s[105];
ignore = scanf(" %s", s);
for (int j = 0; j < n; j++) {
if (s[j] == '1') {
neigh[i].push_back(j);
}
}
}
for (int i = 0; i < n; i++) {
memset(vis, 0, sizeof vis);
dfs(i);
cnt[i]--;
}
long double ans = 0;
for (int i = 0; i < n; i++) {
ans += f(n, cnt[i]);
}
printf("%.15Lf\n", ans);
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
for (int i = 0; i < N; ++i) {
cin >> S[i];
}
double ans = 0;
for (int i = 0; i < N; ++i) {
vector<bool> used(N, false);
queue<int> q;
q.emplace(i);
used[i] = true;
while (!q.empty()) {
int j = q.front();
q.pop();
for (int k = 0; k < N; ++k) {
if (S[k][j] == '1' && used[k] == false) {
q.emplace(k);
used[k] = true;
}
}
}
int cnt = 0;
for (auto e : used) {
if (e) ++cnt;
}
ans += 1.0 / cnt;
}
cout << std::fixed << std::setprecision(15) << ans << endl;
} |
/*
K.D. Vinit |,,|
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
void solve()
{
int n, m;
cin>>n>>m;
int a[n+1][m+1];
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
char x;
cin>>x;
if(x=='.') a[i][j]=0;
if(x=='R') a[i][j]=1;
if(x=='B') a[i][j]=2;
}
}
int cnt[3][n+m+1];
for(int i=1; i<=n+m; i++)
{
cnt[0][i]=0;
cnt[1][i]=0;
cnt[2][i]=0;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
int x = i+j;
int y = a[i][j];
cnt[y][x]++;
}
}
int ans = 1;
for(int i=2; i<=n+m; i++)
{
if(cnt[1][i]!=0 && cnt[2][i]!=0)
{
cout<<0<<endl;
return;
}
if(cnt[2][i]==0 && cnt[1][i]==0)
{
ans*=2;
ans%=mod;
}
}
cout<<ans<<endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
solve();
return 0;
}
| #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <chrono>
#include <random>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
// typedef pair<int,P> IP;
// typedef pair<P,P> PP;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=998244353;
ll mpow(ll x,ll n){
if(n==0) return 1;
else if(n%2) return x*mpow(x,n-1)%MOD;
return mpow(x*x%MOD,n/2)%MOD;
}
int H,W;
char S[500][500];
int main(){
cin>>H>>W;
rep(i,H) rep(j,W) cin>>S[i][j];
ll cnt=0;
rep(k,H+W-1){
int r=0, b=0, d=0;
int j=0;
for(int i=k;i>=0;i--){
j=k-i;
if(0<=i && i<H && 0<=j && j<W){
// cout<<'('<<i<<','<<j<<')'<<endl;
if(S[i][j]=='R') r++;
if(S[i][j]=='B') b++;
if(S[i][j]=='.') d++;
}
}
if(r>0 && b>0){
cout<<0<<endl;
return 0;
}
if(r==0 && b==0) cnt++;
}
ll ans=mpow(2,cnt);
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=a;(i)<(b);++(i))
#define RFOR(i,a,b) for(ll i=a;(i)>=(b);--(i))
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,n,0)
#define ALL(v) v.begin(), v.end()
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)), v.end())
#define BIT(n) (1LL<<(n))
#define DEBUG(a) cerr << #a << " = " << a << endl
const double PI = acos(-1);
const int inf = 1001001001;
const int mod = (int)1e9+7;
//const ll inf = 1e15;
//const ll mod = 1e9+7;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int main() {
int h, w;
cin >> h >> w;
vector<pair<int, int>> ps, d_ps, uniq_ps;
REP(i, h) REP(j, w) {
char c;
cin >> c;
if (c == '#') {
ps.push_back(make_pair(i * 10 - 5, j * 10 - 5));
ps.push_back(make_pair(i * 10 + 5, j * 10 - 5));
ps.push_back(make_pair(i * 10 - 5, j * 10 + 5));
ps.push_back(make_pair(i * 10 + 5, j * 10 + 5));
}
}
sort(ps.begin(), ps.end());
uniq_ps = ps;
UNIQ(uniq_ps);
REP(i, uniq_ps.size()) {
//cout << ps[i].first << " " << ps[i].second << endl;
int cnt = count(ps.begin(), ps.end(), uniq_ps[i]);
if (cnt == 2 || cnt == 4) {
continue;
}
d_ps.push_back(uniq_ps[i]);
//if (ps[i-1] == ps[i] && ps[i+1] == ps[i]) {
// d_ps.push_back(ps[i]);
// continue;
//}
//if (ps[i-1] == ps[i] || ps[i+1] == ps[i]) {
// continue;
//}
//d_ps.push_back(ps[i]);
}
//REP(i, d_ps.size()) {
// cout << d_ps[i].first << " " << d_ps[i].second << endl;
//}
cout << d_ps.size() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) v.begin(), v.end()
template<class T>bool chmax(T& a, const T& b) { if (a<b) { a=b; return 1;} return 0;}
template<class T>bool chmin(T& a, const T& b) { if (b<a) { a=b; return 1;} return 0;}
int main() {
int H, W; cin >> H >> W;
vector<string> S(H); rep(i, H) cin >> S[i];
int dy[] = {0, 0, 1, 1};
int dx[] = {0, 1, 0, 1};
int res = 0;
for (int i = 0; i < H-1; ++i) {
for (int j = 0; j < W-1; ++j) {
int cnt = 0;
for (int k = 0; k < 4; ++k) {
if (S[i+dy[k]][j+dx[k]] == '#') ++cnt;
}
if (cnt == 1 || cnt == 3) {
++res;
}
}
}
cout << res << endl;
} |
#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
void fail() {print("No"); exit(0);}
// clang-format on
int main() {
ll n, m, t;
cin >> n >> m >> t;
//vector<pll> ab(m);
ll n0 = n;
ll curt = 0;
for (ll i = 0; i < m; i++) {
ll a, b;
cin >> a >> b;
n = n - (a - curt);
if (n <= 0) fail();
n = n + (b - a);
n = min(n0, n);
curt = b;
}
n = n - (t - curt);
if (n <= 0) fail();
print("Yes");
}
| #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<set>
#include<unordered_set>
#include<map>
typedef long long int ll;
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define REP(i,n) for (int i=0;i<(n);i++)
#define EREP(i,n) for (int i=1;i<=(n);i++)
#define ALL(a) (a).begin(),(a).end()
//#define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X << ":" <<X<<" " ;
#define TF(f) f ? cout<<"true " : cout<<"false ";
#define END cout<<"\n";
#else
#define DEB(X) {}
#define TF(f) {}
#define END {}
#endif
const ll MOD = 1000000007;
const int INF = 2000000000;
ll N,M,K,T;
ll A[100010],B,C,X;
bool F=true;
ll ans=0;
string S;
int main(){
ios_base::sync_with_stdio(false);
cin>>N>>M>>T;
ll pos=0;
ll res=N;
REP(i,M){
ll a,b;
cin>>a>>b;
if(N-(a-pos)<=0){
cout<<"No"<<endl;
return 0;
}
if(N+b-a-(a-pos)<=res)N+=b-a-(a-pos);
else N=res;
pos=b;
DEB(N)DEB(pos)
}
cout<<(N-(T-pos)>0?"Yes":"No")<<endl;
return 0;
}
|
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
long LCM(long m, long n){
//nPrime = 1000000007
if (m < n){
swap(m,n);
}
if (m % n == 0){
return n;
}
long a = m, b = n;
while (b > 0) {
long t = a / b;
a -= t * b;
swap(a, b);
}
return a;
}
long Inverse(const long m, const long n){
//nPrime = 1000000007
if (m % n == 0){
return 0;
}
long a = m, b = n, u = 1, v = 0;
while (b > 0) {
long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= n;
if (u < 0){
u += n;
}
return u;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
long n,s,k;
scanf("%ld %ld %ld", &n, &s, &k);
long ilcm = LCM(n,k);
if(s % ilcm != 0){
printf("-1\n");
continue;
}
n /= ilcm;
s /= ilcm;
k /= ilcm;
long nAns = Inverse(k,n) * (n-s);
nAns %= n;
// printf("%ld %ld %ld\n", n, s, k);
printf("%ld\n",nAns);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9+7;
// {g,x,y}: ax+by=g
tuple<ll,ll,ll> extgcd(ll a, ll b) {
if (b == 0) return {a,1,0};
ll g, x, y;
tie(g,x,y) = extgcd(b, a%b);
return {g,y,x-a/b*y};
}
void solve() {
ll n, s, k;
cin >> n >> s >> k;
ll g, x, y;
tie(g,x,y) = extgcd(k,n);
if (s%g != 0) {
cout << -1 << endl;
return;
}
n /= g;
s /= g;
k /= g;
ll ans = ((x*-s)%n+n)%n;
cout << ans << endl;
}
int main() {
int t;
cin >> t;
rep(i,t) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
int ans = 0;
for (int i = 1; i <n ; i++)
{
int j = n - i;
if(j>=1)ans++;
}
cout<<ans+1/2<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s;
cin>>s;
if(s.size()%2){
string ss;
for(int i=0;i<s.size()/2;i++){
ss+='9';
}
if(ss.size()==0)ss="0";
cout<<ss;
return 0;
}
string a=s.substr(0,s.size()/2),b=s.substr(s.size()/2,s.size()/2);
int x=stoi(a),y=stoi(b);
cout<<max(x-1,min(x,y));
} |
#include<bits/stdc++.h>
using namespace std;
const int N=1000005,M=998244353;
int n,m,i,mi[21],su[N];
int main()
{
scanf("%d",&n);
for(i=0;i<n;++i)
printf("%d %d\n",i*2%n+1,(i*2+1)%n+1);
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return 0;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return 0;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
#define pq(T) priority_queue<T, vector<T>, greater<T>>
struct Edge {ll to; ll w; Edge(ll to, ll w) : to(to), w(w) {}};
using Graph = vector<vector<Edge>>;
vl dijkstra(Graph &G, int s){
int n = G.size();
vl dist(n, INF);
dist[s] = 0;
using pp = pair<ll, ll>;
pq(pp) que;
que.push({dist[s], s});
while (!que.empty()) {
ll v = que.top().second;
ll d = que.top().first;
que.pop();
if (d > dist[v]) continue;
if(!G[v].empty()) fore(e, G[v]) {
if (chmin(dist[e.to], dist[v] + e.w)) {
que.push({dist[e.to], e.to});
}
}
}
return dist;
}
int n;
int main() {
fio(); cin>>n;
Graph E(n+1);
rep1(i,n){
ll x = (2*i)%n+1, y=(2*i+1)%n+1;
cout<<x<<" "<<y<<"\n";
E[i].emplace_back(x,1);
E[i].emplace_back(y,1);
}
ll ans = 0;
rep1(i,n) {
vl dist = dijkstra(E,i);
ll tmp = 0;
rep1(i,n) chmax(tmp,dist[i]);
chmax(ans,tmp);
}
}
|
#include <cstdio>
#define For(i, l, r) for(i=int(l); i<=int(r); i++)
#define Fo(i, n) For(i, 1, n)
typedef long long int64;
const int MOD = 1e9 + 7;
inline int qpow(int64 b, int p, int mod=MOD) {
int res=1;
while(p) {
if(p & 1)
res=res*b%mod;
b=b*b%mod;
p>>=1;
}
return res;
}
int main() {
int i, sum = 0;
int x, invx;
int n, m;
scanf("%d %d", &n, &m);
Fo(i, n) {
scanf("%d", &x);
sum += x;
}
sum += n;
m += n;
if(sum > m) {
printf("0\n");
return 0;
}
x = invx = 1;
Fo(i, sum) {
x = x * (m - i + 1LL) % MOD;
invx = 1LL * invx * i % MOD;
}
printf("%lld\n", 1LL * x * qpow(invx, MOD - 2) % MOD);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m, sum = 0, ans;
cin >> n >> k >> m;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
sum += p;
}
if (m * n - sum < 0)
ans = 0;
else if (k < m * n - sum)
ans = -1;
else
ans = m * n - sum;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(string s1,string s2)
{
int x=0,y=0,n1=s1.size(),n2=s2.size();
for(int i=0,f=0;i<n1;++i)
{
if(!f&&s1[i]=='0') continue;
else f=1,x=(x<<3)+(x<<1)+(s1[i]^48);
}
for(int i=0,f=0;i<n2;++i)
{
if(!f&&s2[i]=='0') continue;
else f=1,y=(y<<3)+(y<<1)+(s2[i]^48);
}
return x-y;
}
signed main()
{
int n,k,res;cin>>n>>k;res=n;
for(int i=1;i<=k;++i)
{
string s1=to_string(res);
sort(s1.begin(),s1.end());
string s2=s1;
reverse(s1.begin(),s1.end());
res=solve(s1,s2);
}
cout<<res;return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll powk(ll a, ll n = 2){
ll r = 1;
for(ll i = 0;i < n;i++){
r *= a;
}
return r;
}
int main()
{
ll n, k;
cin >> n >> k;
ll t = n;
for(ll i = 0;i < k;i++){
string st;
st = to_string(t);
sort(st.begin(),st.end());
ll m = 0, u = 0;
for(ll j = 0;j < st.size();j++){
m += pow(10, j) * (st[st.size() - j - 1] - '0');
u += pow(10, j) * (st[j] - '0');
}
t = u - m;
}
cout << t;
return 0;
} |
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define ll long long
//#define file
using namespace std;
int n,K,i,j,k,l,L,R,Mid,sum,s1,s2;
int a[400001][2],ls[200001],len;
int f[200001];
void New(int x,int y) {++len;a[len][0]=y;a[len][1]=ls[x];ls[x]=len;}
void dfs(int Fa,int t)
{
int i,s1=n+1,s2=-n-1;
bool bz=0;
f[t]=0;
for (i=ls[t]; i; i=a[i][1])
if (a[i][0]!=Fa)
{
dfs(t,a[i][0]),bz=1;
s1=min(s1,f[a[i][0]]+1);
s2=max(s2,f[a[i][0]]+1);
f[t]=max(f[t],f[a[i][0]]+1);
}
if (bz)
{
if (s1+1<0 && s1+1+s2<=0) f[t]=s1;
else
{
f[t]=s2;
if (f[t]==Mid)
f[t]=-Mid-1,++sum;
}
}
}
int main()
{
#ifdef file
freopen("e.in","r",stdin);
#endif
scanf("%d%d",&n,&K);
fo(i,1,n-1) scanf("%d%d",&j,&k),New(j,k),New(k,j);
L=1,R=n;
while (L<R)
{
Mid=(L+R)/2;
sum=0;
dfs(0,1);
s1=n+1,s2=-n-1;
for (i=ls[1]; i; i=a[i][1])
{
s1=min(s1,f[a[i][0]]+1);
s2=max(s2,f[a[i][0]]+1);
}
if (!(f[1]<0 || s1+1+s2<=0)) ++sum;
if (sum>K) L=Mid+1; else R=Mid;
}
printf("%d\n",L);
fclose(stdin);
fclose(stdout);
return 0;
} | //Target for Pupil
//Implementation and Data structures
//Prodip sarker(student_ip)
#define bn begin()
#define en end()
#define mx 5000010
#define pb push_back
#define PI (2.0*acos(0.0))
#define al(a) a.begin(),a.end()
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rep(a,b) for(int a=0;a<b;++a)
#define fastio ios::sync_with_stdio(false)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
long long unsigned a,b,c,x,y,n,i,t,tmp,d,e,f,p;
cin>>x>>y>>a>>b;
ll sum=0;
while(x<y)
{
// cout<<x<<"\n";
if(LLONG_MAX/x >= a &&x*a<x+b&&(x*a<y))
{
sum++;
x=x*a;
}
else
{
y-=1;
sum+=(y-x)/b;
break;
}
}
cout<<sum<<endl;
return 0;
} |
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=2e5+5, INF=1e9+5, mod=998244353;
ll pot(ll a, ll b){
if(b==0) return 1ll;
if(b==1) return a;
ll c=pot(a, b/2);
c=(c*c)%mod;
return (c*pot(a, b&1))%mod;
}
int main(){
int a, b, k;
cin>>a>>b>>k;
if(a==1){
cout<<pot(k, b);
}
else if(b==1){
cout<<pot(k, a);
}
else{
ll ans=0;
for(int i=1; i<=k+1; i++){
ans+=(pot(i, a)-pot(i-1, a))*1ll*pot(k+1-i, b);
ans%=mod;
}
cout<<(ans+mod)%mod;
}
}
| #include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define inf 1000000000
#define pii pair<int,int>
#define fi first
#define se second
#define inp(a) for(auto &x: a)cin>>x
#define fr(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) for(int i=a;i<b;++i)
int binpow(int a,int b,int m){
a%=m;
int res=1;
while(b>0){
if(b&1)res=res*a%m;
a=a*a%m;b/=2;
}
return res;
}
signed main(){
int a,b,c,mod=998244353;
cin>>a>>b>>c;
if(a==1){cout<<binpow(c,b,mod);return 0;}
if(b==1){cout<<binpow(c,a,mod);return 0;}
int ans=0;
for(int i=1;i<=c;i++){
ans=(ans+binpow(c-i+1,b,mod)*((binpow(i,a,mod)-binpow(i-1,a,mod)+mod)%mod)%mod)%mod;
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
using LL = long long;
using pii = std::pair<int, int>;
using pll = std::pair<LL, LL>;
int main() {
//freopen("in","r",stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
LL s, p;
std::cin >> s >> p;
__int128 S = s, P = p;
if (S * S < 4 * P) {
std::cout << "No\n";
return 0;
}
__int128 b = std::sqrt(1.0 * S * S - 4 * P + 0.5);
if (b * b == S * S - 4 * P && ((b + s) % 2 == 0)) {
std::cout << "Yes\n";
} else std::cout << "No\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(*array)
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 int INF = 1000100100;
const int MOD = 1000000007;
vector<long long> divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
}
}
sort(ret.begin(), ret.end());
return ret;
}
ll S, P;
int main() {
cin >> S >> P;
vector<ll> div = divisor(P);
for(ll i = 0; i < div.size(); ++i) {
if(div.at(i) + (P/div.at(i)) == S) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
int n,k;
cin >> n >> k;
int a[n];
for(int i=0;i<n;i++)
cin >> a[i];
vector<int> fre(300001,0);
for(int i=0;i<n;i++)
fre[a[i]]++;
vector<int> mn_fre(300001);
ll ans = 0;
for(int i=0;i<=300000;i++)
{
if(i == 0)
mn_fre[i] = min(k,fre[i]);
else
{
mn_fre[i] = min(mn_fre[i-1],fre[i]);
ans += (mn_fre[i] - mn_fre[i-1]<0)*((mn_fre[i-1] - mn_fre[i])*i);
}
}
cout << ans << endl;
return 0;
}
| #include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<sstream>
#include<complex>
#include<iomanip>
#include<numeric>
#include<cassert>
#include<random>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < (int)(Y);++(X))
#define reps(X,S,Y) for (int (X) = (int)(S);(X) < (int)(Y);++(X))
#define rrep(X,Y) for (int (X) = (int)(Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (int)(Y)-1;(X) >= (int)(S);--(X))
#define repe(X,Y) for ((X) = 0;(X) < (Y);++(X))
#define peat(X,Y) for (;(X) < (Y);++(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define Endl endl
#define NL <<"\n"
#define cauto const auto
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
//#undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
void ouT(ll x,int d=3){auto re=to_string(x);if((int)re.size()>d) re=x>0?"oo":"-oo";cout<<string(d-re.size(),' ')<<re<<",";}
#ifdef __cpp_init_captures
template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);}
template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));}
#endif
template<class A,class B> pair<A,B> operator+(const pair<A,B> &p,const pair<A,B> &q){ return {p.X+q.X,p.Y+q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator+=(pair<A,B> &p,const pair<C,D> &q){ p.X+=q.X; p.Y+=q.Y; return p;}
template<class A,class B> pair<A,B> operator-(const pair<A,B> &p,const pair<A,B> &q){ return {p.X-q.X,p.Y-q.Y};}
template<class A,class B,class C,class D> pair<A,B>& operator-=(pair<A,B> &p,const pair<C,D> &q){ p.X-=q.X; p.Y-=q.Y; return p;}
template<class A,class B> istream& operator>>(istream &is, pair<A,B> &p){ is>>p.X>>p.Y; return is;}
template<class T=ll> T read(){ T re; cin>>re; return move(re);}
template<class T=ll> T read(const T &dec){ T re; cin>>re; return re-dec;}
template<class T=ll> vector<T> readV(const int sz){ vector<T> re(sz); for(auto &x:re) x=read<T>(); return move(re);}
template<class T=ll> vector<T> readV(const int sz, const T &dec){ vector<T> re(sz); for(auto &x:re) x=read<T>(dec); return move(re);}
vv<int> readG(const int &n,const int &m){ vv<int> g(n); rep(_,m){ cauto a=read<int>(1),b=read<int>(1); g[a].pb(b); g[b].pb(a);} return move(g);}
vv<int> readG(const int &n){ return readG(n,n-1);}
#define TT cauto TTT=read();rep(_,TTT)
const ll MOD=1e9+7; //998244353;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cout<<fixed<<setprecision(0);
cauto n=read();
cauto t=read();
cauto vs=readV(n);
vector<int> cnt(n+1);
for(cauto &v:vs) ++cnt[v];
ll re=0;
rep(_,t){
int i=0;
for(;cnt[i];++i) --cnt[i];
re+=i;
}
cout<<re NL;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
string S, T;
cin >> S >> T;
deque<int> A, B;
for (int i = 0; i < N; i++) {
if (S[i] == '1') A.emplace_back(i);
if (T[i] == '1') B.emplace_back(i);
}
long long ans = 0;
for (int i = 0; i < N; i++) {
int s = binary_search(begin(A), end(A), i);
int t = T[i] - '0';
if (s == t) {
if (s == 1) {
assert(A.front() == i);
A.pop_front();
}
continue;
}
if (s == 0 && t == 1) {
if (A.empty()) {
cout << -1 << '\n';
return 0;
}
ans += abs(A.front() - i);
A.pop_front();
} else if (s == 1 && t == 0) {
assert(A.front() == i);
A.pop_front();
if (A.empty()) {
cout << -1 << '\n';
return 0;
}
ans += abs(A.front() - i);
A.pop_front();
} else {
assert(false);
}
}
cout << ans << '\n';
return 0;
}
| #include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
typedef unsigned long long ull;
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void sc(int &x){scanf("%d",&x);}
void sc(int &x,int &y){scanf("%d%d",&x,&y);}
void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void sc(ll &x){scanf("%lld",&x);}
void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);}
void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);}
void sc(char *x){scanf("%s",x);}
void sc(char *x,char *y){scanf("%s%s",x,y);}
void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);}
void out(int x){printf("%d\n",x);}
void out(ll x){printf("%lld\n",x);}
void out(int x,int y){printf("%d %d\n",x,y);}
void out(ll x,ll y){printf("%lld %lld\n",x,y);}
void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);}
void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);}
using namespace std;
const int N=2e5+5;
int n,a[N],b[N];
unordered_map<int,int>vis;
vector<int>v[N];
int bit[N];
void add(int x)
{
while(x<=n) bit[x]++,x+=x&-x;
}
int query(int x)
{
int ans=0;
while(x) ans+=bit[x],x-=x&-x;
return ans;
}
int main()
{
sc(n);
rep(i,1,n) sc(a[i]),a[i]+=i-1;
rep(i,1,n) sc(b[i]),b[i]+=i-1;
int id=0;
rep(i,1,n)
if(!vis[a[i]])
vis[a[i]]=++id;
nep(i,n,1)
v[vis[a[i]]].push_back(i);
ll ans=0;
rep(i,1,n)
{
int x=vis[b[i]];
if(!x||v[x].size()==0)
{
out(-1);return 0;
}
int pos=v[x].back()+(i-1-query(v[x].back()));
add(v[x].back());
v[x].pop_back();
ans+=pos-i;
}
out(ans);
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i =0; i <(n); i++)
using ll = long long;
int main(){
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
int ans = 1<<30;
rep(s,1<<(n-1)){
int now = 0;
int o = 0;
rep(i,n){
o |= a[i];
if(s>>i&1){
now ^= o;
o = 0;
}
}
now ^= o;
ans = min(ans,now);
}
cout << ans;
}
| #include<bits/stdc++.h>
int main(){
using namespace std;
unsigned long N;
cin >> N;
vector<unsigned long> A(N), B(N);
for(auto&& i : A)cin >> i;
for(auto&& i : B)cin >> i;
priority_queue<unsigned long> pq;
reverse(begin(A), end(A));
unsigned long ans{0};
for(unsigned long i{0}; i < N; ++i){
pq.emplace(A.back());
pq.emplace(B.back());
A.pop_back();
B.pop_back();
ans += pq.top();
pq.pop();
}
cout << ans << endl;
return 0;
} |
#include <iostream>
using namespace std;
int main(){
int n, x; char c;
cin >> n >> x;
for (int i=1; i<=n; i++){
cin >> c;
if (c == 'o') x++;
else if (c == 'x'){
if (x != 0) x--;
else continue;
}
}
cout << x << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template<int M>
struct static_mint {
static_assert(0 < M, "Module must be positive");
int val;
static_mint(): val() {}
static_mint(long long x) : val(x % M) { if (val < 0) val += M; }
static_mint pow(long long n) const { static_mint ans = 1, x(*this); while (n) { if (n & 1) ans *= x; x *= x; n /= 2; } return ans; }
static_mint inv() const { return pow(M - 2); }
friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); }
friend static_mint inv(const static_mint &m) { return m.inv(); }
static_mint operator+() const { static_mint m; m.val = val; return m; }
static_mint operator-() const { static_mint m; m.val = M - val; return m; }
static_mint &operator+=(const static_mint &m) { if ((val += m.val) >= M) val -= M; return *this; }
static_mint &operator-=(const static_mint &m) { if ((val -= m.val) < 0) val += M; return *this; }
static_mint &operator*=(const static_mint &m) { val = (long long) val * m.val % M; return *this; }
static_mint &operator/=(const static_mint &m) { val = (long long) val * m.inv().val % M; return *this; }
friend static_mint operator+ (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) += rhs; }
friend static_mint operator- (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) -= rhs; }
friend static_mint operator* (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) *= rhs; }
friend static_mint operator/ (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) /= rhs; }
friend bool operator==(const static_mint &lhs, const static_mint &rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const static_mint &lhs, const static_mint &rhs) { return lhs.val != rhs.val; }
static_mint &operator++() { return *this += 1; }
static_mint &operator--() { return *this -= 1; }
static_mint operator++(int) { static_mint result(*this); *this += 1; return result; }
static_mint operator--(int) { static_mint result(*this); *this -= 1; return result; }
template <typename T> explicit operator T() const { return T(val); }
friend std::ostream &operator<<(std::ostream &os, const static_mint &m) {
return os << m.val;
}
friend std::istream &operator>>(std::istream &is, static_mint &m) {
long long x; is >> x; m = x;
return is;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
using mint = static_mint<998244353>;
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto &x : a) cin >> x;
vector<int> mask(n + m - 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'R') {
mask[i + j] |= 1;
}
if (a[i][j] == 'B') {
mask[i + j] |= 2;
}
}
}
mint ans = 1;
for (int lvl = 0; lvl < n + m - 1; lvl++) {
if (mask[lvl] == 3) {
ans = 0;
} else if (mask[lvl] == 0) {
ans *= 2;
}
}
cout << ans << '\n';
return 0;
} |
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <tuple>
#include <cassert>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long ll;
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
int main ()
{
ll A,B;
cin >> A >> B;
vector<ll> ans;
REP(i,A) ans.push_back(i + 1);
REP(i,B) ans.push_back(-(i + 1));
ll Asum = 0, Bsum = 0;
REP(i,A) Asum += i + 1;
REP(i,B) Bsum += i + 1;
ll diff = abs(Asum - Bsum);
if (Asum < Bsum) {
ans[A - 1] += diff;
} else if (Asum > Bsum) {
ans[A + B - 1] -= diff;
}
REP(i,ans.size()) {
cout << ans[i] << ((i < ans.size() - 1) ? " " : "\n");
}
return 0;
}
| #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(int i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 1e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
ll mpow(ll a, ll b){
if(b >= (MOD-1)) b %= (MOD-1);
if(b==0) return 1;
ll t1 = mpow(a,b/2);
t1 *= t1;
t1 %= MOD;
if(b%2) t1 *= a;
t1 %= MOD;
return t1;
}
ll mpow(ll a, ll b, ll p){
if(b==0) return 1;
ll t1 = mpow(a,b/2,p); t1 *= t1;
t1 %= p;
if(b%2) t1 *= a;
t1 %= p;
return t1;
}
ll modinverse(ll a, ll m){
ll m0 = m;
ll y = 0, x = 1;
if (m == 1) return 0;
while (a > 1){
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
ll range(ll l, ll r){
return l + rand()%(r-l+1);
}
void solve(){
ll a,b;
cin>>a>>b;
ll x=0;
if(a==1 && b!=1){
forsn(i, 1, b+1){
cout<<-1*i<<" ";
x+=i;
}
cout<<x;
return;
}
if(a!=1 && b==1){
forsn(i, 1, a+1){
cout<<i<<" ";
x+=i;
}
cout<<-1*x;
return;
}
if(a>b){
forsn(i, b, a+1){
x+=i;
}
forsn(i, 1, a+1){
cout<<i<<" ";
}
forsn(i, 1, b){
cout<<-1*i<<" ";
}
cout<<-1*x;
}
else if(a<b){
forsn(i, a, b+1){
x+=i;
}
forsn(i, 1, a){
cout<<i<<" ";
}
forsn(i, 1, b+1){
cout<<-1*i<<" ";
}
cout<<x;
}
else{
forsn(i, 1, a+1){
cout<<i<<" ";
}
forsn(i, 1, b+1){
cout<<-1*i<<" ";
}
}
}
int main()
{
fast_cin();
ll t=1;
// cin>>t;
forn(i,t) {
// cout << "Case #" << i+1 << ": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// --------------------------------------------------------
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 FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((ll)(c).size())
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << #x << " = " << (x) << '\n';
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
// static const int INF = (1 << 30) - 1; // 1073741824 - 1
static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
ll N; cin >> N;
VLL A(N); REP(i,N) cin >> A[i];
ll ans = 0;
ll total = 0;
ll S = 0;
ll max_S = -INF;
REP(i,N) {
S += A[i];
chmax(max_S, S);
chmax(ans, total + max_S);
total += S;
}
COUT(ans);
return 0;
}
| #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <type_traits>
#include <cmath>
#include <cassert>
#include <cstdio>
using namespace std;
using namespace placeholders;
using LL = long long;
using ULL = unsigned long long;
using VI = vector< int >;
using VVI = vector< vector< int > >;
using VS = vector< string >;
using ISS = istringstream;
using OSS = ostringstream;
using PII = pair< int, int >;
using VPII = vector< pair< int, int > >;
template < typename T = int > using VT = vector< T >;
template < typename T = int > using VVT = vector< vector< T > >;
template < typename T = int > using LIM = numeric_limits< T >;
template < typename T = int > using OSI = ostream_iterator< T >;
template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; }
template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; }
void in_impl(){};
template < typename T, typename... TS > void in_impl( T &head, TS &... tail ){ cin >> head; in_impl( tail ... ); }
#define IN( T, ... ) T __VA_ARGS__; in_impl( __VA_ARGS__ );
template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); }
#define NUMBERED( name, number ) NUMBERED2( name, number )
#define NUMBERED2( name, number ) name ## _ ## number
#define REP1( n ) REP2( NUMBERED( REP_COUNTER, __LINE__ ), n )
#define REP2( i, n ) REP3( i, 0, n )
#define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i )
#define GET_REP( a, b, c, F, ... ) F
#define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2, REP1 )( __VA_ARGS__ )
#define FOR( e, c ) for ( auto &&e : c )
#define ALL( c ) begin( c ), end( c )
#define AALL( a ) ( remove_all_extents< decltype( a ) >::type * )a, ( remove_all_extents< decltype( a ) >::type * )a + sizeof( a ) / sizeof( remove_all_extents< decltype( a ) >::type )
#define SZ( v ) ( (int)( v ).size() )
#define EXISTS( c, e ) ( ( c ).find( e ) != ( c ).end() )
template < typename T > inline bool chmin( T &a, const T &b ){ if ( b < a ) { a = b; return true; } return false; }
template < typename T > inline bool chmax( T &a, const T &b ){ if ( a < b ) { a = b; return true; } return false; }
#define PB push_back
#define EM emplace
#define EB emplace_back
#define BI back_inserter
#define MP make_pair
#define fst first
#define snd second
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
// Λ Λ__
// /(*゚ー゚)/\
// /|  ̄U U ̄|\/
// | |/
int main()
{
cin.tie( nullptr );
ios::sync_with_stdio( false );
cout << setprecision( 12 ) << fixed;
IN( LL, N );
if ( N == 2 )
{
cout << 1 << endl;
return 0;
}
LL lb = 0, ub = sqrt( N ) * 2;
while ( lb + 1 < ub )
{
const LL mid = ( lb + ub ) / 2;
( mid * ( mid + 1 ) / 2 <= N + 1 ? lb : ub ) = mid;
}
cout << N + 1 - lb << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
int H, W, Abuf;
int minNum = 101;
int sumNum = 0;
cin >> H >> W;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cin >> Abuf;
minNum = min(minNum, Abuf);
sumNum +=Abuf;
}
}
cout << sumNum - minNum*H*W << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
using ll = long long int;
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
const long long INF = 1LL << 60;
const int inf = 1001001001;
const ll mod = 1000000007;
int main()
{
int h, w;
cin >> h >> w;
vi a(h*w); rep(i,h*w) cin >> a[i];
sort(ALL(a));
int min = a[0];
int res = 0;
for (int i = 1; i < h*w; i++) {
res += a[i] - min;
}
cout << res << endl;
} |
#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;
int main(){
int N;cin>>N;
int max=0;
veci a;
int maxa=0;
int gcd;
rep(i,0,N){
int A;cin>>A;
a.push_back(A);
maxa=maxa>A?maxa:A;
}
rep(i,2,maxa+1){
int at_sum=0;
rep(j,0,N){
if(a[j]%i==0)at_sum++;
}
if(max<at_sum){
gcd=i;
max=at_sum;
}
}
cout<<gcd<<endl;
} | #include<iostream>
int main(){
int max_gcd = 0, ans = 0;
int n;
std::cin >> n;
int a[n];
for(int i=0; i < n; i++){
std::cin >> a[i];
}
for(int h=2; h < 1000; h++){
int gcd=0;
for(int i=0; i < n; i++){
if(a[i] % h == 0){
gcd++;
}
}
if(gcd > max_gcd){
max_gcd = gcd;
ans = h;
}
}
if(!ans){
std::cerr << "error" << std::endl;
}else{
std::cout << ans << std::endl;
}
} |
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define e "\n"
#define fl(x,n) for(ll i=x;i<=n;i++)
#define fl2(x,n) for(ll i=x;i>=n;i--)
using namespace std;
int main()
{
ll n;
cin >> n;
if(n%100==0)
cout << 100 << e;
else
{
ll d=ceil((double)n/100);
cout << d*100-n << e;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int X;
cin >> X;
if (X % 100 == 0) {
cout << 100 << endl;
}
else {
cout << 100 - X % 100 << endl;
}
return 0;
} |
/////////////////////////////////////////////////////////////
// Give me AC!!! //
////////////////////////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, c, n) for (int i = c; i < (n); ++i)
#define yn(ok, True, False) if (ok) { p(True) } else {p(False)};
//以下debug用
#define d(x) cout << #x << "; " << x << endl;
#define p(x) cout << x << endl;
#define f(x) \
for (int i = 0; i < x.size(); i++) \
cout << #x << "[" << i << "]; " << x[i] << endl;
int a, b, c, i, j, m, n, M, N, L, R, ans = 0, counter = 0, num = 0;
long long llans = 0, llcounter = 0, llnum = 0;
string str, str1, str2, str3;
bool ok = false;
int main()
{
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, 0, h) cin >> s[i];
rep(i,0,h-1)rep(j,0,w-1){
int cnt = 0;
rep(di, 0, 2) rep(dj, 0, 2) if (s[i + di][j + dj] == '#') cnt++;
if (cnt ==1 || cnt == 3)
++ans;
}
p(ans)
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
char x[n][m];
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
cin>>x[i][j];
int num1=0;
for (int i=0;i<n-1;i++){
for (int j=0;j<m-1;j++){
int num2=0;
if (x[i][j]=='#')num2++;
if (x[i][j+1]=='#')num2++;
if (x[i+1][j]=='#')num2++;
if (x[i+1][j+1]=='#')num2++;
if (num2==1 || num2==3)num1++;
}
}
cout<<num1;
}
|
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define cm (1 << 17)
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
if (ci - cn == cm) {
fread(cn, 1, cm, stdin);
ci = cn;
}
return *ci++;
}
inline int getint() {
int A = 0;
while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
return A;
}
inline int up(const void* a, const void* b) {
return *(int*)a < *(int*)b ? -1 : *(int*)a > *(int*)b ? 1 : 0;
}
inline void sortup(int* a, int n) {
qsort(a, n, sizeof(int), up);
}
#define mxN 200005
int h[mxN], w[mxN];
long long sum_l[mxN], sum_r[mxN];
int main() {
int n = getint(), m = getint(), pos = 0;
for (int i = 0; i < n; i++) {
h[i] = getint();
}
for (int i = 0; i < m; i++) {
w[i] = getint();
}
sortup(h, n);
sortup(w, m);
sum_l[0] = sum_r[0] = 0;
for (int i = 0; i < n / 2; i++) {
sum_l[i + 1] = sum_l[i] + h[2 * i + 1] - h[2 * i];
sum_r[i + 1] = sum_r[i] + h[2 * i + 2] - h[2 * i + 1];
}
long long ans = 1LL << 60, right = sum_r[n / 2];
for (int i = 0; i < m; i++) {
while (pos < n && h[pos] < w[i]) pos++;
ans = min(ans, sum_l[pos / 2] + right - sum_r[pos / 2] +
abs(h[pos - pos % 2] - w[i]));
}
printf("%lld", ans);
} | #include <bits/stdc++.h>
using namespace std;
template <int mod = (int)(1e9 + 7)>
struct ModInt {
int x;
constexpr ModInt() : x(0) {}
constexpr ModInt(int64_t y)
: x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
constexpr ModInt &operator+=(const ModInt &p) noexcept {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
constexpr ModInt &operator-=(const ModInt &p) noexcept {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
constexpr ModInt &operator*=(const ModInt &p) noexcept {
x = (int)(1LL * x * p.x % mod);
return *this;
}
constexpr ModInt &operator/=(const ModInt &p) noexcept {
*this *= p.inverse();
return *this;
}
constexpr ModInt operator-() const { return ModInt(-x); }
constexpr ModInt operator+(const ModInt &p) const noexcept {
return ModInt(*this) += p;
}
constexpr ModInt operator-(const ModInt &p) const noexcept {
return ModInt(*this) -= p;
}
constexpr ModInt operator*(const ModInt &p) const noexcept {
return ModInt(*this) *= p;
}
constexpr ModInt operator/(const ModInt &p) const noexcept {
return ModInt(*this) /= p;
}
constexpr bool operator==(const ModInt &p) const noexcept { return x == p.x; }
constexpr bool operator!=(const ModInt &p) const noexcept { return x != p.x; }
constexpr ModInt inverse() const noexcept {
int a = x, b = mod, u = 1, v = 0, t = 0;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
constexpr ModInt pow(int64_t n) const {
ModInt res(1), mul(x);
while (n) {
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
friend constexpr ostream &operator<<(ostream &os, const ModInt &p) noexcept {
return os << p.x;
}
friend constexpr istream &operator>>(istream &is, ModInt &a) noexcept {
int64_t t = 0;
is >> t;
a = ModInt<mod>(t);
return (is);
}
constexpr int get_mod() { return mod; }
};
using mint = ModInt<>;
int n;
vector<long long> b;
vector<vector<mint>> dp;
mint solve();
int main() {
cin >> n;
b.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) {
cin >> b[i];
b[i] += b[i - 1];
}
cout << solve() << endl;
return 0;
}
mint solve() {
mint res = 0;
dp.assign(n + 1, vector<mint>(n + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = n; j >= 1; --j) {
mint now = dp[j - 1][b[i] % j];
if (j != n) dp[j][b[i] % (j + 1)] += now;
if (i == n) res += now;
}
}
return res;
} |
#include<bits/stdc++.h>
using namespace std;
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define All(x) (x).begin(),(x).end()
#define mp make_pair
#define nl "\n"
typedef pair<int,int>ii;
typedef vector<int>vi;
typedef vector<ii>vii;
typedef vector<vi>vvi;
typedef vector<long long>vl;
typedef vector <vl>vvl;
template<class T>
void print(T& a)
{
for(auto x:a)
cout<<x<<" ";
cout<<"\n";
}
const ll MOD=1e9+7;
ll bnp(ll n, ll p)
{
ll res=1;
while(p)
{
if(p&1)res=res*n%MOD;
n=n*n%MOD;
p>>=1;
}
if(res<0)res+=MOD;
return res;
}
const ll MX=2e6+5;
ll fact[MX],inv[MX];
void fac()
{
fact[0]=fact[1]=inv[0]=inv[1]=1;
for(ll i=2; i<MX; i++)
{
fact[i]=fact[i-1]*i%MOD;
inv[i]=bnp(fact[i],MOD-2);
}
}
int main()
{
FastIO
fac();
ll n,m,k; cin>>n>>m>>k;
ll ans,tem;
if(n>m+k)
{
cout<<0<<nl;
return 0;
}
ans=1ll*fact[n+m]*inv[m]%MOD*inv[n]%MOD;
if(n-k-1>=0)
{
tem=1ll*fact[n+m]*inv[m+k+1]%MOD;
tem=1ll*tem*inv[n-k-1]%MOD;
ans=(ans-tem)%MOD;
}
if(ans<0)ans+=MOD;
cout<<ans<<nl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
// Defines
#define all(x) (x).begin(),(x).end()
#define sortall(x) sort(x.begin(),x.end())
#define maxin(x) *max_element(x.begin(),x.end())
#define minin(x) *min_element(x.begin(),x.end())
#define lint long long int
#define nl '\n'
#define INF (lint)1e18
#define EPS (long double)1/(INF)
//#define MOD (lint)998244353
#define MOD (lint)(1e9+7)
//Debugs
#ifdef LOCAL
#define debugvar(x) cout<<#x<<"\t:"<<(x)<<"\n"
#define debugvec(x) cout<<#x<<"\t:"; for(auto it = (x).begin();it!=(x).end();it++) cout<<*it<<" "; cout<<nl
#else
#define debugvar(x) 0
#define debugvec(x) 0
#endif
//Templates
template <typename T> inline T gcd(T a, T b) { a=(a>0?a:-a); b=(b>0?b:-b); while (b != 0) swap(b, a %= b); return a;}
template <typename T> inline T extended_gcd(T a, T& x, T b, T& y){ // returns gcd(a,b) && a*x + b*y = gcd(a,b)
T l_a = 1, l_b = 0;
T r_a = 0, r_b = 1;
T q = a/b, r = a%b;
while(r>0){
T temp;
temp = r_a, r_a = l_a - q*r_a ,l_a = temp;
temp = r_b, r_b = l_b - q*r_b ,l_b = temp;
a = b;
b = r;
q = a/b;
r = a%b;
}
x = r_a;
y = r_b;
return b;
}
template <typename T> inline T MOD_inv (T num){
lint inv,temp;
assert(extended_gcd(MOD,temp,num,inv)==1);
return (inv%MOD+MOD)%MOD;
}
template <typename T> inline T lcm(T a, T b) { if(a*b==0) return (T)0; return ((a/gcd(a,b))*b);}
template <typename T> inline T getpow(T base, T exp){
if(exp==0) return (T)1;
T half = getpow(base,exp/2);
T ans = half*half;
if(exp&1) ans = ans*base;
return ans;
}
template <typename T> inline T getpowMOD(T base, T exp){
if(exp==0) return (T)1;
base = base%MOD;
T half = getpowMOD(base,exp/2);
T ans = (half*half)%MOD;
if(exp&1) ans = (ans*base)%MOD;
return ans;
}
lint ncrMOD(lint n, lint r){
if(r>n) return 0;
r = min(r,n-r);
lint fact = 1;
for(lint i=1;i<=r;i++){
fact=(fact*(n+1-i))%MOD;
fact=(fact*MOD_inv(i))%MOD;
}
return fact;
}
void solve(){
lint n,m,k;
cin>>n>>m>>k;
if(m-n<-k){cout<<0;return;}
lint ans = ncrMOD(m+n,m)-ncrMOD(m+n,m+k+1);
ans = (ans+MOD)%MOD;
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);lint testnum=1,totalnum=1;
//cin>>totalnum;
for(testnum=1;testnum<=totalnum;++testnum){
//cout<<"Case #"<<testnum<<": ";
//cout<<ncrMOD(28,14);
solve();
cout<<nl;
}
bool time = 0;
//time=true;
if(time){
std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_taken = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
cout << "\nTime Taken: " <<setprecision(10)<<time_taken.count() << " s";
}
return 0;
}
/*
created on: 27.06.2021
*/
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
// template starts
using namespace __gnu_pbds; //required
using namespace std;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// ordered_set <int> s;
// s.find_by_order(k); returns the (k+1)th smallest element
// s.order_of_key(k); returns the number of elements in s strictly less than k
#define MOD (1000000000+7) // change as required
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(), x.end()
#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;
#define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;
typedef long long int ll;
// start of highly risky #defines
#define int ll // disable when you want to make code a bit faster
#define endl '\n' // disable when dealing with interactive problems
// End of highly risky #defines
// template ends here
void solve(){
// code starts from here
int a,b,c,d;
cin >> a >> b >> c >> d;
double ans = a*d + b*c;
ans /= (b+d);
cout << setprecision(20) << ans << endl;
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//startTime = clock();
int T;
//cin >> T;
T = 1;
while(T--){
solve();
}
//cout << getCurrentTime() << endl;
return 0;
}
| #include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main()
{
long double sx,sy,gx,gy;
cin>>sx>>sy>>gx>>gy;
long double ans;
ans=((sx*gy)+(sy*gx))/(sy+gy);
cout.precision(10);
cout<<ans<<endl;
return 0;
}
|
// abc193_d
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#include "../../debug_util/timer.hpp"
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e15)
#define MOD (1000000007)
#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;
int main() {
#ifdef LOCAL
ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif
int K;
cin >> K;
string S, T;
cin >> S >> T;
ll rest = 9*K-8;
ll numerator = 0;
ll denominator = rest * (rest - 1);
map<int, int> mp_s;
REP(i, 4) mp_s[S[i]-'0']++;
map<int, int> mp_t;
REP(i, 4) mp_t[T[i]-'0']++;
// print(mp_s, mp_t);
REPN(i, 1, 10) REPN(j, 1, 10){
ll p = K - mp_s[i] - mp_t[i];
ll q = K - mp_s[j] - mp_t[j];
if (i == j) q--;
mp_s[i]++;
mp_t[j]++;
ll x = 0;
REPN(k, 1,10) x += k * pow(10, mp_s[k]);
ll y = 0;
REPN(k, 1,10) y += k * pow(10, mp_t[k]);
// print(i, j, p, q, x, y);
if (p > 0 && q > 0 && x > y){
numerator += p * q;
}
mp_s[i]--;
mp_t[j]--;
}
// print(numerator, denominator);
double ans = 1.0 * numerator / denominator;
print(ans);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for(ll i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
ll T, X, Y, P, Q;
ll extgcd(ll a, ll b, ll& x, ll &y){
ll d = a;
if(b != 0){
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}else{
x = 1; y = 0;
}
return d;
}
ll gcd(ll a, ll b){
ll x, y;
return extgcd(a, b, x, y);
}
ll mod_inverse(ll a, ll m){
ll x, y;
extgcd(a, m, x, y);
return (m + x % m) % m;
}
int main(){
cin >> T;
REP(cs, T){
cin >> X >> Y >> P >> Q;
ll ans = -1;
ll d = gcd(2 * X + 2 * Y, P + Q), minv = mod_inverse((2 * X + 2 * Y) / d, (P + Q) / d);
FOR(t, P - (X + Y - 1), P + Q - X){
if(t % d != 0) continue;
ll t1;
if(t >= P - X){
t1 = X;
}else{
t1 = P - t;
}
ll x = t1 + (minv * ((t % (P + Q) + (P + Q)) % (P + Q)) / d) % ((P + Q) / d) * (2 * X + 2 * Y);
if(ans == -1){
ans = x;
}else{
ans = min(ans, x);
}
}
if(ans == -1){
cout << "infinity" << endl;
}else{
assert(ans >= 0);
cout << ans << endl;
}
}
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Kein Yukiyoshi
*/
#include "bits/stdc++.h"
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FOR(i, a) for (auto &i : a)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0LL)
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))
using namespace std;
const int MOD = (int)(1e9 + 7);
const int INF = (int)(1e13 + 7);
const double EPS = 1e-14;
const double PI = acos(-1);
int CEIL(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } //ceil() for int
int mod(int a, int b) { return a >= 0 ? a % b : a - (b * CEIL(a, b)); } //always return positive num
int bpm(int a, int b) { //return x^y in order(log(y))
int res = 1;
for (a %= MOD; b; a = a * a % MOD, b >>= 1)
if (b & 1) res = res * a % MOD;
return res;
}
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {} //0からn-1
int find(int x) { return (d[x] < 0 ? x : d[x] = find(d[x])); }
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
class BValues {
public:
static void solve(istream &cin, ostream &cout) {
int N, M;
cin >> N >> M;
vector<int> a(N);
vector<int> b(N);
rep(i, N) cin >> a[i];
rep(i, N) cin >> b[i];
if (SUM(a) != SUM(b)) {
cout << "No" << endl;
return;
}
UnionFind temp(N + 100);
rep(i, M) {
int c, d;
cin >> c >> d;
temp.unite(c, d);
}
map<int, pair<int, int>> check;
REP(i, 1, N + 1) {
check[temp.find(i)].first += a[i-1];
check[temp.find(i)].second += b[i-1];
}
FOR(i, check) {
if (i.second.second != i.second.first) {
cout << "No" << endl;
return;
}
}
cout << "Yes" << endl;
}
};
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cout<<fixed<<setprecision(15);
BValues solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define data data123
const int N = 1005;
vector<int> G[N*N];
int n, m;
int pos(int i, int j) {
return (i-1)*n+j;
}
vector<pii> data[N*N];
bool vis[N*N];
void solve() {
scanf("%d%d", &n, &m);
char ch[5];
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d%s", &u, &v, ch);
data[u].push_back(pii(v, ch[0]-'a'));
data[v].push_back(pii(u, ch[0]-'a'));
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (auto v1: data[i]) {
for (auto v2: data[j]) {
if (v1.second == v2.second) {
G[pos(i, j)].push_back(pos(v1.first, v2.first));
}
}
}
}
}
queue<pii> que;
for (int i = 1; i <= n; i++) {
que.push(pii(pos(i, i), 0));
vis[pos(i, i)] = 1;
}
for (int i = 1; i <= n; i++) {
for (auto v: data[i]) {
que.push(pii(pos(i, v.first), 1));
vis[pos(i, v.first)] = 1;
}
}
while (!que.empty()) {
pii t = que.front(); que.pop();
int u = t.first;
int x = ((u % n == 0) ? (u/n) : (u/n+1));
int y = ((u % n == 0) ? n : (u%n));
if ((x == 1 && y == n) || (x == n && y == 1)) {
printf("%d\n", t.second);
return;
}
for (auto v: G[u]) {
if (!vis[v]) que.push(pii(v, t.second+2)), vis[v] = 1;
}
}
printf("-1\n");
}
int main(){
solve();
return 0;
} |
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
int n, m, c, a[2];
long long ans;
string s;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> s;
c = 0;
for (int j = 0; j < m; j++) {
c += (s[j] - 48);
}
if (c & 1)
ans += a[0], a[1]++;
else
ans += a[1], a[0]++;
}
cout << ans << endl;
}
| #include <iostream>
using namespace std;
int main() {
int n, m; cin >> n >> m;
int a[2] = {0, 0};
for (int i = 0; i < n; i++) {
string s; cin >> s;
int t = 0;
for (char c : s) if (c == '1') t ^= 1;
a[t]++;
}
cout << (long long)a[0] * a[1] << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int N = 3e3 + 5;
const int mod = 1e9 + 7;
const int INF = 1e18;
const double eps = 1e-5;
const double PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct mi {
int v; explicit operator int() const { return v; }
mi() { v = 0; }
mi(ll _v):v(_v%mod) { v += (v<0)*mod; }
};
mi& operator+=(mi& a, mi b) {
if ((a.v += b.v) >= mod) a.v -= mod;
return a; }
mi& operator-=(mi& a, mi b) {
if ((a.v -= b.v) < 0) a.v += mod;
return a; }
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); }
mi& operator*=(mi& a, mi b) { return a = a*b; }
mi pow(mi a, ll p) { assert(p >= 0); // asserts are important!
return p==0?1:pow(a*a,p/2)*(p&1?a:1); }
mi inv(mi a) { assert(a.v != 0); return pow(a,mod-2); }
mi operator/(mi a, mi b) { return a*inv(b); }
mi dp[N][N];
int prv[N][N];
int s[N], ind[N];
void solve(int tc){
int n;
cin >> n;
vector<mi> a(n+1), pref(n+1);
for(int i=1; i<=n; i++) cin >> a[i].v;
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
s[i] = (s[i-1] + a[i].v)%k;
}
for(int i=0; i<k; i++){
ind[i] = -1;
}
for(int i=0; i<=n; i++){
prv[k][i] = ind[s[i]];
ind[s[i]] = i;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(prv[j][i] != -1){
dp[i][j] = dp[prv[j][i]][j-1] + dp[prv[j][i]][j];
}
// cout << prv[i][j] << ' ';
}
// cout << endl;
}
mi ans = 0;
for(int j=1; j<=n; j++)
ans += dp[n][j];
cout << ans.v << '\n';
}
int32_t main(){
fast;
int tc = 1;
// cin >> tc;
for(int tt=1; tt<=tc; tt++){
solve(tt);
}
return 0;
} | #include<iostream>
#include<vector>
using namespace std;
using ll=long long;
const ll MOD=1000000007;
int main(){
int n;cin>>n;
vector<ll>sum(n+1,0);
for(int i=0;i<n;i++){
ll a;cin>>a;
sum.at(i+1)=sum.at(i)+a;
}
vector<vector<int>>idx(n+1,vector<int>(n+1,-1));
vector<vector<int>>pre(n+1,vector<int>(n+1,-1));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
idx.at(i).at(j)=pre.at(j).at(sum.at(i)%j);
pre.at(j).at(sum.at(i)%j)=i;
}
}
vector<vector<ll>>dp(n+1,vector<ll>(n+1,0));
for(int i=1;i<=n;i++)dp.at(i).at(1)=1;
ll ans=0;
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
if(idx.at(j).at(i)!=-1){
int x=idx.at(j).at(i);
dp.at(j).at(i)=dp.at(x).at(i)+dp.at(x).at(i-1);
dp.at(j).at(i)%=MOD;
}
}
ans+=dp.at(n).at(i);
ans%=MOD;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
// template {{{
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(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
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
constexpr i32 mod = 1e9 + 7;
// constexpr i32 mod = 998244353;
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); }
// }}}
void solve() {
int n = input();
vector< string > ans;
ans.emplace_back("AB");
range(i, 1, n) {
vector< string > na;
for (auto &a : ans) {
auto b = a;
na.emplace_back(a + b);
for (auto &c : b) {
if (c == 'A') c = 'B';
else c = 'A';
}
na.emplace_back(a + b);
}
na.emplace_back(string(ans[0].size(), 'A') + string(ans[0].size(), 'B'));
ans = na;
}
cout << ans.size() << endl;
for (auto &a : ans) cout << a << endl;
}
signed main() {
solve();
}
| #include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds;
//typedef tree<long long,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//typedef tree<long long, null_type, less_equal<>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
#define ll long long
#define inf 1000000000000000000
#define ff first
#define ss second
#define f(i,a,b) for(ll i=a;i<b;i++)
#define it(b) for(auto &it:b)
#define NUM 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
long long binpow(long long a, long long b) {
if (b == 0)
return 1;
long long res = binpow(a, b / 2)%NUM;
if (b % 2)
return (((res * res)%NUM)* a)%NUM;
else
return (res * res)%NUM;
}
inline ll min(ll a,ll b)
{
if(a>b)return b;return a;
}
inline ll max(ll a, ll b)
{
if(a>b)return a;return b;
}
inline ll dif(long long a,long long b)
{
if(a>b)
return a-b;
return b-a;
}
long long gcd(long long a,long long b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
long long k=gcd(a,b);return (a*b)/k;
}
void read(vll &arr,int n)
{
if(arr.size()!=n){arr.resize(n);}
for(int i=0;i<n;i++)cin>>arr[i];
}
void sieveOferatosthenes(long long MAX)
{
vector<long long >prime;vll lps(MAX+1);
vector<bool> pri(MAX+1,true);
pri[1]=false;
lps[1]=1;lps[2]=2;
for (long long p = 2; p * p <= MAX; p++) {
if (pri[p]) {
lps[p] = p;
for (long long i = p * p; i <= MAX; i += p) {
pri[i] = false;
if (lps[i] == 0) {
lps[i] = p;
}
}
}
}
for(long long i=1;i<=MAX;i++) {
if (pri[i]) {
prime.push_back(i);
lps[i]=i;
}
}
}
void solve()
{
ll n;cin>>n;
map<ll,ll>m;
f(i,0,n) {
ll a;
cin >> a;
m[a]++;
}
ll res=0;
it(m)
{
res+=(it.ss*(n-it.ss));
}
res/=2;
cout<<res<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << showpoint;
cout << setprecision(6);
int t;t=1;
///cin>>t;
//WE WILL WIN.
while(t--)
{
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n;
typedef pair<int, int> pint;
pint a[N], b[N], c[N];
int product(pint a, pint b) {
return a.first * b.second - a.second * b.first;
}
pint diff(pint b, pint a) {
return {b.first - a.first, b.second - a.second};
}
int sqr(int x) {
return x * x;
}
int dist(pint a, pint b) {
return sqr(a.first - b.first) + sqr(b.second - a.second);
}
void _sort(pint a[]) {
pint diff_root = diff(a[2], a[1]);
sort(a + 3, a + n + 1, [&a, diff_root](const pint &x, const pint &y) {
pint diff_x = diff(x, a[1]);
pint diff_y = diff(y, a[1]);
int product_x = product(diff_x, diff_root);
int product_y = product(diff_y, diff_root);
int dist_x = dist(x, a[1]);
int dist_y = dist(y, a[1]);
if (product_x != product_y) {
return product_x < product_y;
} else {
return dist_x < dist_y;
}
});
}
bool check() {
for (int i = 2; i <= n; ++ i) {
if (dist(a[i], a[1]) != dist(c[i], c[1])) {
return false;
}
}
pint v_a = diff(a[2], a[1]);
pint v_c = diff(c[2], c[1]);
for (int i = 3; i <= n; ++ i) {
pint x_a = diff(a[i], a[1]);
pint x_c = diff(c[i], c[1]);
if (product(x_a, v_a) != product(x_c, v_c)) {
return false;
}
}
return true;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i].first >> a[i].second;
}
for (int i = 1; i <= n; ++ i) {
cin >> b[i].first >> b[i].second;
}
if (n == 1) {
cout << "Yes\n";
return 0;
}
_sort(a);
for (int root = 1; root <= n; ++ root) {
for (int rear = 1; rear <= n; ++ rear) {
if (rear == root) {
continue;
}
c[1] = b[root];
c[2] = b[rear];
int index = 3;
for (int i = 1; i <= n; ++ i) {
if (i != rear && i != root) {
c[index ++] = b[i];
}
}
_sort(c);
if (check()) {
cout << "Yes";
return 0;
}
}
}
cout << "No";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-6;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N;
cin >> N;
vc<complex<ld>> S(N), T(N);
rep(i, N) {
ld a, b;
cin >> a >> b;
S[i] = {a, b};
}
rep(i, N) {
ld a, b;
cin >> a >> b;
T[i] = {a, b};
}
complex<ld> sc(0, 0), tc(0, 0);
rep(i, N) sc += S[i], tc += T[i];
sc /= N, tc /= N;
rep(i, N){
S[i] -= sc;
T[i] -= tc;
}
rep(i, N) rep(k, 2){
vc<complex<ld>> ss(N), tt(N);
rep(si, N) ss[si] = S[si] * polar(1.0L, -arg(S[k]));
rep(ti, N) tt[ti] = T[ti] * polar(1.0L, -arg(T[i]));
bool ok = true;
rep(si, N) {
bool isok = false;
rep(ti, N){
if (abs(ss[si] - tt[ti]) < EPS) isok = true;
}
if(!isok){
ok = false;
break;
}
}
if(ok){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} |
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using Pair = pair<int, int>;
using lint = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
// const int mod = 1e9 + 7;
// const int mod = 998244353;
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;
}
inline void Yes(bool condition) {
if (condition) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
}
int main() {
int n;
string s;
cin>>n>>s;
vector<vector<int>> cnt(n+1, vector<int>(4, 0));
rep(i,n){
if (s[i] == 'A') cnt[i+1][0]++;
else if(s[i]=='T')
cnt[i+1][1]++;
else if(s[i]=='C')
cnt[i+1][2]++;
else
cnt[i+1][3]++;
}
rep(i,n){
rep(j,4){
cnt[i + 1][j] += cnt[i][j];
}
}
int ret = 0;
for (int i = 1; i <= n;i++) {
for (int j = i; j <= n; j++) {
if(cnt[j][0]-cnt[i-1][0]==cnt[j][1]-cnt[i-1][1]&&
cnt[j][2]-cnt[i-1][2]==cnt[j][3]-cnt[i-1][3]){
ret++;
//cout << i << " " << j << endl;
}
}
}
cout << ret << endl;
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<ll> H(N), W(M);
rep(i, N) { cin >> H[i]; }
rep(i, M) { cin >> W[i]; }
sort(all(H));
sort(all(W));
vector<ll> suml(N), sumr(N);
rep(i, N - 1) {
suml[i] = abs(H[i + 1] - H[i]);
i++;
}
rep(i, N - 1) { suml[i + 1] += suml[i]; }
for (int i = N - 1; i > 0; i--) {
sumr[i] = abs(H[i] - H[i - 1]);
i--;
}
for (int i = N - 1; i > 0; i--) {
sumr[i - 1] += sumr[i];
}
ll ans = INF;
rep(i, M) {
ll itr = lower_bound(all(H), W[i]) - H.begin(), sum = 0;
if (itr % 2 == 0) {
sum += abs(H[itr] - W[i]);
int l = itr - 1, r = itr + 1;
if (l >= 0) {
sum += suml[l];
}
if (r < N) {
sum += sumr[r];
}
} else {
sum += abs(H[itr - 1] - W[i]);
int l = itr - 2, r = itr + 1;
if (l >= 0) {
sum += suml[l];
}
if (r < N) {
sum += sumr[r];
}
}
ans = min(ans, sum);
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define endl "\n"
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];}
#define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl;
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++)
#define mem(ar,x) memset(ar,x,sizeof(ar));
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define MAX 1000000000000000000
#define MAX1 1000000000
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
inline int solve()
{
int n,m;
cin>>n>>m;
map <int , vector <int> > mp;
fr(i,0,m){
int x,y;
cin>>x>>y;
mp[x].push_back(y);
}
set <int> st;
st.insert(n);
for(auto it : mp){
set <int> a,b;
for(auto ut : it.second){
int x = ut;
if(st.find(x) != st.end() && st.find(x-1) == st.end() && st.find(x+1) == st.end()){
b.insert(x);
}
if(st.find(x) == st.end() && (st.find(x-1) != st.end() || st.find(x+1) != st.end()) ){
a.insert(x);
}
}
for(auto ut : a){
st.insert(ut);
}
for(auto ut : b){
st.erase(ut);
}
}
cout << st.size() << endl;
rt;
}
signed main()
{
// fastio;
int t;
int test_cases=1;
if(test_cases==0)
cin>>t;
else
t=1;
while(t--){
solve();
}
} | #include <iostream>
#include <vector>
#include <algorithm>
const int INF = 0x3fffffff;
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> H(n);
vector<int> W(m);
for (int i = 0; i < n; i++)
cin >> H[i];
for (int i = 0; i < m; i++)
cin >> W[i];
sort(H.begin(), H.end());
sort(W.begin(), W.end());
vector<int> sum1((n + 1) / 2);
vector<int> sum2((n + 1) / 2);
for (int i = 0; i + 1 < n; i += 2) {
int j = i + 1;
sum1[i / 2 + 1] = sum1[i / 2] + H[i+1] - H[i];
sum2[j / 2 + 1] = sum2[j / 2] + H[j+1] - H[j];
}
long long ans = 100000000000;
for (auto w : W) {
int idx = lower_bound(H.begin(), H.end(), w) - H.begin();
long long sum = 0;
if (idx % 2 != 0) {
idx--;
}
sum += abs(H[idx] - w);
sum += sum1[idx / 2] + sum2[n / 2] - sum2[idx / 2];
ans = min(ans, sum);
}
printf("%lld\n", ans);
return 0;
}
|
#include<stdio.h>
double dp[101][101][101];
double f(int a,int b,int c){
if(dp[a][b][c])return dp[a][b][c];
if(a==100||b==100||c==100)return 0;
double ans=0;
ans+=(f(a+1,b,c)+1)*a/(a+b+c);
ans+=(f(a,b+1,c)+1)*b/(a+b+c);
ans+=(f(a,b,c+1)+1)*c/(a+b+c);
dp[a][b][c]=ans;
return ans;
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%.9f\n",f(a,b,c));
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
long long A, B, C;
cin >> A >> B >> C;
auto power = []( long long x, long long n, long long m )
{
long long res = 1;
while( n > 0 ) {
if( n % 2 ) res = res * x % m;
x = x * x % m;
n /= 2;
}
return res;
};
long long bc = power( B, C, 4 );
long long ans = power( A, bc + 4, 10 );
cout << ans << endl;
}
|
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.12f\n", x)
const double pi = acos(-1.0);
const ll MOD = 998244353;
const ll INF = 1e18;
const ll MAX_N = 1010101;
//組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_N; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// mod.m での a の逆元 a^ { -1 } を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//最大公約数
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)return 0;
return (x / gcd(x, y) * y);
}
//union-find木について
//サイズによる更新をしている
ll par[201010];
ll rank2[201010];
void init(ll n) {
rep(i, n) {
par[i] = i;
rank2[i] = 1;
}
}
ll find(ll x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)return;
if (rank2[x] < rank2[y]) {
par[x] = y;
rank2[y] += rank2[x];
}
else {
par[y] = x;
rank2[x] += rank2[y];
}
}
ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll dp[110][110];
vector<vector<pair<ll, ll>>>to;
bool flag[101010];
ll dist[101010];
void dfs(ll v) {
flag[v] = true;
for (auto nv : to[v]) {
ll nv2 = nv.first;
ll dist2 = nv.second;
if (flag[nv2] == false) {
dist[nv2] = dist[v] + dist2;
dfs(nv2);
}
}
}
ll c[1010][1010];
ll a[1010], b[1010];
//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll n;
cin >> n;
init(n);
rep(i, n) {
ll x;
cin >> x;
x--;
unite(i, x);
}
map<ll, ll>mp;
rep(i, n) {
mp[find(i)]++;
}
ll res = 0;
for (auto p : mp) {
res++;
}
ll ans = 0;
COMinit();
for (ll i = 1;i <= res;++i) {
ans += COM(res, i);
ans %= MOD;
}
cout << ans << endl;
return 0;
} | #include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define Int register int
#define Swap(a, b) a ^= b, b ^= a, a ^= b
#define Abs(x) ((x) < 0 ? -(x) : (x))
#define Max(x, y) ((x) < (y) ? (y) : (x))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Isdigit(ch) (ch >= '0' and ch <= '9')
const LL MOD = 1e9 + 7;
typedef pair <int, int> T;
const int MAXN = 1e3 + 10, MAXM = 1e6 + 10, INF = 1e9;
inline LL Read () {
LL f = 0, x = 0;
char ch = getchar ();
while (!isdigit (ch) ) {
f |= (ch == '-'), ch = getchar ();
}
while (isdigit (ch) ) {
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar ();
}
return f ? -x : x;
}
inline void Write (const LL &x) {
if (x < 0 ) {
putchar ('-'), Write (-x);
return ;
}
if (x > 9 ) {
Write (x / 10);
}
putchar ((x % 10) ^ 48);
return ;
}
LL t, n, m;
inline LL Qkpow (LL Base, LL x) {
LL Total = 1;
while (x ) {
if (x & 1 ) {
Total = Total * Base % MOD;
}
x >>= 1, Base = Base * Base % MOD;
}
return Total;
}
inline LL C(LL n, LL m) {
n = Min (n, m - n);
LL Answer = 1;
for (LL i = 1; i <= n; ++ i ) {
Answer = Answer * Qkpow (i, MOD - 2) % MOD;
}
for (LL i = m; i ^ (m - n); -- i ) {
Answer = Answer * i % MOD;
}
return Answer;
}
signed main () {
t = n = Read (), m = Read () + n;
for (LL i = 1; i <= t; ++ i ) {
n += Read ();
}
if (m < n ) {
Write (0), putchar ('\n');
return 0;
}
Write (C (n, m)), putchar ('\n');
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------
int N,P;
const ll mo=1000000007;
ll modpow(ll a, ll n = mo-2) {
ll r=1;a%=mo;
while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
return r;
}
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N>>P;
cout<<(P-1)*modpow(P-2,N-1)%mo<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| #include<cstdio>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;
#define gc() getchar()
inline bool ig(char c){return c>='0'&&c<='9';}
inline void read(ll &oi){char c;ll f=1,res=0;while(c=gc(),(!ig(c))&&c^'-');c^'-'?res=(c^48):f=-1;while(c=gc(),ig(c))res=res*10+(c^48);oi=f*res;}
inline void print(ll oi){if(oi<0)putchar('-'),oi=~oi+1;if(oi>9)print(oi/10);putchar(oi%10+48);}
ll n,m;
inline ll fastmul(ll a,ll b){
ll res=1;
while(b){
if(b%2)res*=a,res%=mod;
a*=a;a%=mod;b>>=1;
}
return res%mod;
}
int main(){
read(n);read(m);print((m-1)*fastmul(m-2,n-1)%mod);putchar('\n');
return 0;
} |
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename T = int> using V = vector<T>;
template<typename T = int> using VV = vector<vector<T>>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
os << '(' << a.first << ',' << a.second << ')';
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for (auto x : v) os << x << ' ';
return os;
}
void debug() {
cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
cerr << a;
if (sizeof...(b)) cerr << ", ";
debug(b...);
}
class UnionFind {
private:
int sz;
vector<int> par, size_;
public:
UnionFind(){}
UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
iota(par.begin(), par.end(), 0);
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y){
x = find(x); y = find(y);
if(x == y) return;
if(size_[x] < size_[y]) swap(x,y);
par[y] = x;
size_[x] += size_[y];
}
int size(int x){
x = find(x);
return size_[x];
}
bool same(int x, int y){
return find(x) == find(y);
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<ll>a(n), b(n);
rep(i,n)cin >> a[i];
rep(i,n)cin >> b[i];
UnionFind uf(n);
while(m--){
int c,d;
cin >> c >> d;
uf.unite(c-1,d-1);
}
vector<ll>sum(n);
vector<ll>sum2(n);
rep(i,n){
sum[uf.find(i)] += a[i];
sum2[uf.find(i)] += b[i];
}
if(sum == sum2){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
}
| #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>
#define FOR_LT(i, beg, end) for (decltype(end) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(end) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n) for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)
using namespace std;
class UnionFind
{
public:
UnionFind(int min, int max)
: elems_(max - min + 1)
, min_(min)
, rank_(max - min + 1)
{
for (int i = min; i <= max; i++) {
Elem& elem = this->elems_[i - min];
elem.i = i;
elem.num = 1;
}
}
int find(int i) {
int idx = i - this->min_;
if (this->elems_[idx].i == i) return i;
int root = this->find(this->elems_[idx].i);
this->elems_[idx].i = root; // 簡略化
return root;
}
int size(int i) {
int root = this->find(i);
int idx = root - this->min_;
return this->elems_[idx].num;
}
int rank() {
return rank_;
}
void merge(int i, int j) {
int i_root = this->find(i);
int j_root = this->find(j);
if (i_root == j_root) return;
int i_idx = i_root - this->min_;
int j_idx = j_root - this->min_;
int i_size = this->elems_[i_idx].num;
int j_size = this->elems_[j_idx].num;
if (i_size < j_size) {
this->elems_[i_idx].i = j_root;
this->elems_[j_idx].num += this->elems_[i_idx].num;
}
else {
this->elems_[j_idx].i = i_root;
this->elems_[i_idx].num += this->elems_[j_idx].num;
}
this->rank_--;
}
private:
struct Elem {
int i;
int num; // root 以外は無効
};
vector<Elem> elems_;
int min_;
int rank_;
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int64_t n, m; cin >> n >> m;
vector<int64_t> as(n + 1);
FOR_LE(i, 1, n) {
cin >> as[i];
}
vector<int64_t> bs(n + 1);
FOR_LE(i, 1, n) {
cin >> bs[i];
}
UnionFind uf(1, n);
REP(m) {
int a, b; cin >> a >> b;
uf.merge(a, b);
}
unordered_map<int, vector<int>> gm;
FOR_LE(i, 1, n) {
int g = uf.find(i);
gm[g].push_back(i);
}
for (auto& g : gm) {
int64_t asum = 0;
int64_t bsum = 0;
for (auto i : g.second) {
asum += as[i];
bsum += bs[i];
}
if (asum != bsum) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
//const int mod = 998244353;
signed main(){
int N; cin >> N;
vec x(N), y(N);
REP(i, N) cin >> x[i] >> y[i];
v_Pii px(N), py(N);
REP(i, N){
px[i] = Pii(x[i], i);
py[i] = Pii(y[i], i);
}
Sort(px);
Sort(py);
set<int> cand;
REP(k, 2){
cand.insert(px[k].se);
cand.insert(px[N - 1 - k].se);
cand.insert(py[k].se);
cand.insert(py[N - 1 - k].se);
}
vec ds;
REP(i, N){
for(int j: cand) if(j < i) ds.pb(max(llabs(x[i] - x[j]), llabs(y[i] - y[j])));
}
Sort(ds);
Out(ds[SZ(ds) - 2]);
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk make_pair
#define pb push_back
#define eb emplace_back
#define ins insert
#define fi first
#define se second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=(1<<20)+5,Mod=998244353;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
//inline char gc(){return getchar();}
inline int read(){
int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}
int main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
int a=read(),b=read(),c=read();
cout<<(1ll*a*(a+1)/2)%Mod*((1ll*b*(b+1)/2)%Mod)%Mod*((1ll*c*(c+1)/2)%Mod)%Mod;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int,int>::iterator
#define sit set<int>::iterator
#define itrm(g,x) for(mit g=x.begin();g!=x.end();g++)
#define itrs(g,x) for(sit g=x.begin();g!=x.end();g++)
#define ltype int
#define rep(i,j,k) for(ltype(i)=(j);(i)<=(k);(i)++)
#define rap(i,j,k) for(ltype(i)=(j);(i)<(k);(i)++)
#define per(i,j,k) for(ltype(i)=(j);(i)>=(k);(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
#define check(x) if(x>=mod) x-=mod
const int inf=0x3f3f3f3f,mod=1000000007;
const double pi=3.1415926535897932,eps=1e-6;
void chmax(int &x,int y){if(x < y) x = y;}
void chmin(int &x,int y){if(x > y) x = y;}
int qpow(int x,int y){
int ret = 1;
while(y) {
if(y & 1) ret = (ll)ret * x % mod;
x = (ll)x * x % mod;
y >>= 1;
}
return ret;
}
int n;ll k,dp[4][3000005],pre[3000005];
int main()
{
cin>>n>>k;
dp[0][0] = 1;rep(i,0,n+n+n) pre[i] = 1;
rep(i,1,3) {
rep(j,1,n+n+n) dp[i][j] = pre[j - 1] - (j <= n ? 0 : pre[j - n - 1]);
pre[0] = dp[i][0];rep(j,1,n+n+n) pre[j] = pre[j - 1] + dp[i][j];
}
//rep(i,0,3) re[()]
rep(sum,3,n+n+n) {
if(k > dp[3][sum]) k -= dp[3][sum];
else {
rep(a,1,n) {
ll cur = sum <= a ? 0 : dp[2][sum - a];
if(k > cur) k -= cur;
else {
rep(b,1,n) if(1 <= sum - a - b && sum - a - b <= n) {
k--;
if(!k) {printf("%d %d %d\n",a,b,sum-a-b);return 0;}
}
}
}
break;
}
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef tuple<int,int,int> ti;
typedef tuple<ll,ll,ll> tl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<ti> vti;
typedef vector<tl> vtl;
typedef priority_queue<int> pq;
typedef priority_queue<ti,vector<ti>,greater<ti>> pqg;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n,k;
cin>>n>>k;
vl dp(2*n+1,0);
dp[2]=1;
for(ll i=3; i<=2*n; i++){
dp[i]=dp[i-1];
if(i>n) dp[i]+=(n-(i-n)+1);
else dp[i]+=i-1;
}
vl dp2(3*n+1,0);
dp2[3]=1;
for(ll i=4; i<=3*n; i++){
dp2[i]=dp2[i-1];
dp2[i]+=dp[min(i-1,2*n)]-dp[max(1LL,i-n)-1];
}
ll sum;
for(ll i=3; i<=3*n; i++){
if(dp2[i-1]<k) sum=i;
else break;
}
ll cnt=dp2[sum-1];
ll ans;
for(ll i=1; i<=n; i++){
if(sum-i>2*n) continue;
ll prevcnt=cnt;
cnt+=dp[sum-i]-dp[sum-i-1];
if(prevcnt<k) ans=i;
else break;
}
if(ans!=n) cnt-=dp[sum-(ans+1)]-dp[sum-(ans+1)-1];
cnt-=dp[sum-ans]-dp[sum-ans-1];
ll ans2;
for(ll i=1; i<=n; i++){
if(sum-i-ans>n) continue;
ll prevcnt=cnt;
cnt++;
if(prevcnt<k) ans2=i;
else {
cnt=prevcnt;
break;
}
}
cout<<ans<<" "<<ans2<<" "<<sum-ans-ans2;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline bool read(T&x){
x=0;char c=getchar();int f=1;while(!isdigit(c)&&(c!='-')&&(c!=EOF))c=getchar();
if(c==EOF)return 0;if(c=='-')f=-1,c=getchar();
while(isdigit(c)){x=x*10+(c&15);c=getchar();}x*=f;return 1;
}template<typename T,typename...Args>inline bool read(T&x,Args&...args){bool res=1;res&=read(x);res&=read(args...);return res;}
typedef long long LL;typedef unsigned long long u64;typedef unsigned u32;typedef long double LD;
#define il inline
#define pln putchar('\n')
#define For(i,a,b) for(int i=(a),(i##i)=(b);i<=(i##i);++i)
#define Rep(i,n) for(int i=0,(i##i)=(n);i<(i##i);++i)
#define Fodn(i,a,b) for(int i=(a),(i##i)=(b);i>=(i##i);--i)
const int M=1000000007,INF=0x3f3f3f3f;const long long INFLL=0x3f3f3f3f3f3f3f3fLL;
const int N=100007;
int n,a[N][7],ma;
il bool f(int x){
bool g[37];
memset(g,0,sizeof g);
For(i,1,n){
u32 u=0;
For(j,1,5)if(a[i][j]>=x)u|=(1<<(j-1));
g[u]=1;
}
for(u32 i=0;i<=31;++i)if(g[i])
for(u32 j=i;j<=31;++j)if(g[j]){
u32 u=(i|j)^31u;
if(u==0)return 1;
Rep(s,32)if(g[s]&&((s&u)==u))return 1;
}
return 0;
}
inline void init(){
read(n);For(i,1,n)For(j,1,5)read(a[i][j]),ma=max(a[i][j],ma);
}
inline void solve(){
LL l=0,r=ma;
while(l<r){
LL mid=(l+r+1)/2;
if(f(mid))l=mid;else r=mid-1;
}
printf("%lld\n",l);
}
signed main(){
init();solve();
return 0;
} | // YATIN KWATRA
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#define ll long long
#define ar array
#define sz(v) (int)(v.size())
#define inf 1e18
#define int ll
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld long double
#define ull unsigned long long
#define endl "\n"
#define fo(i,a,b) for(int i = a; i<=b ; i++)
#define rfo(i,a,b) for(int i = a; i>=b ; i--)
#define vii vector<int>
#define pq priority_queue
#define uomii unordered_map<int,int,best_hash>
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int,int>
#define mii map<int,int>
#define vvii vector<vii>
#define mod 1000000007
#define MIN -1e9
#define pi 3.1415926535897932384626433832795
#define cz(x) 63 - __builtin_clzll(x)
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct best_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = 3e3 + 1;
int dp[N][N];
int v[N];
vii has[N][N]; // pos for remainder j from after i
int n;
int ways(int pos, int k) {
if (pos == n + 1) return 1;
int &ans = dp[pos][k];
if (ans != -1) return ans;
ans = 0;
// now go to next k+1 or k
int rem = v[pos - 1] % k;
int idx = lower_bound(all(has[k][rem]), pos) - has[k][rem].begin();
if (idx < sz(has[k][rem])) {
int to = has[k][rem][idx];
if (to < n) ans = (ways(to + 1, k) + ways(to + 1, k + 1)) % mod;
else ans = ways(to + 1, k);
}
return ans;
}
void solve()
{
cin >> n;
fo(i, 1, n) cin >> v[i];
memset(dp, -1, sizeof(dp));
fo(i, 1, n) {
int sum = 0;
fo(j, 1, n) {
sum += v[j];
has[i][sum % i].pb(j);
}
}
fo(i, 1, n) v[i] += v[i - 1];
cout << ways(1, 1);
}
signed main() {
FIO
int t;
t = 1;
//cin >> t;
fo(i, 0, t - 1) {
solve();
}
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int maxn = 3e5;
const int inf = 0x3f3f3f3f;
struct B{
int l, v;
}p[maxn];
int N, M, Min[maxn], w[15], temp_w[15], sum[15], used[15];
int d[15][15], dis[15], Minw, ans, vis[15];
bool operator < (const B& a,const B& b){
return a.l < b.l;
}
void init(){
cin >> N >> M;
Minw = inf;
ans = inf;
for(int i = 1; i <= N; ++i) cin >> w[i];
for(int i = 1; i <= M; ++i){
cin >> p[i].l >> p[i].v;
Minw = min(Minw, p[i].v);
}
sort(p + 1, p + 1 + M);
Min[M] = p[M].v;
for(int i = M - 1; i >= 1; --i){
Min[i] = min(Min[i + 1], p[i].v);
}
}
int bin_search(int x){
int l = 1, r = M + 1;
while (l < r){
int mid = (l + r) >> 1;
if(Min[mid] < x) l = mid + 1;
else r = mid;
}
return p[l - 1].l;
}
int Caculate() {
for(int i = 1; i <= N; ++i){
sum[i] = temp_w[i] + sum[i - 1];
}
memset(dis, 0, sizeof(dis));
for(int i = 1; i <= N; ++i){
for(int j = i + 1; j <= N; ++j){
dis[j] = max(dis[i] + bin_search(sum[j] - sum[i - 1]), dis[j]);
}
}
return dis[N];
}
void dfs(int cnt){
if(cnt == N + 1){
ans = min(ans, Caculate());
return;
}
for(int i = 1; i <= N; ++i){
if(!vis[i]){
vis[i] = 1;
temp_w[cnt] = w[i];
dfs(cnt + 1);
vis[i] = 0;
}
}
}
signed main(){
init();
for(int i = 1; i <= N; ++i){
if(w[i] > Minw){
cout << -1;
return 0;
}
}
dfs(1);
cout << ans;
return 0;
} | #include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define int long long
int gi() {
char cc = getchar() ; int cn = 0, flus = 1 ;
while( cc < '0' || cc > '9' ) { if( cc == '-' ) flus = - flus ; cc = getchar() ; }
while( cc >= '0' && cc <= '9' ) cn = cn * 10 + cc - '0', cc = getchar() ;
return cn * flus ;
}
const int inf = 1e15 + 5 ;
const int N = 10 ;
const int M = 1e5 + 5 ;
int n, w[N], Id[N], b[N], f[N], m ;
struct node {
int l, v ;
node(int _l = 0, int _v = 0) { l = _l, v = _v ; }
bool operator < ( const node &x ) const {
return (l == x.l) ? v > x.v : l < x.l ;
}
} l[M] ;
set<node> S ;
set<node>::iterator it ;
signed main()
{
n = gi(), m = gi() ; int mx = 0, mi = inf ;
rep( i, 1, n ) w[i] = gi(), Id[i] = i, mx = max(mx, w[i]) ;
rep( i, 1, m ) l[i].v = gi(), l[i].l = gi(), mi = min( mi, l[i].l ) ;
if( mx > mi ) { puts("-1") ; exit(0) ; }
sort(l + 1, l + m + 1) ;
S.insert(node(-1, 0)), S.insert(l[1]) ;
rep( i, 2, m ) l[i].v = max( l[i - 1].v, l[i].v ) ;
rep( i, 2, m ) S.insert(l[i]) ;
int Ans = inf ;
do {
b[0] = 0 ;
rep( i, 1, n ) b[i] = w[Id[i]] ;
rep( i, 1, n ) b[i] += b[i - 1] ;
rep( i, 1, n ) f[i] = 0 ;
rep( i, 2, n ) {
f[i] = f[i - 1] ;
rep( j, 1, i - 1 ) {
it = S.upper_bound(node(b[i] - b[j - 1], inf)) ;
-- it ; node u = *it ;
f[i] = max( f[i], f[j] + u.v ) ;
}
}
Ans = min( Ans, f[n] ) ;
} while(next_permutation(Id + 1, Id + n + 1)) ;
cout << Ans << endl ;
return 0 ;
} |
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
ll t, N;
cin >> t >> N;
ll p = 100 + t;
vector<bool> vec_ori(p,true);
for(int i=0 ; i<100 ; ++i){
vec_ori[p*i/100] = false;
}
vector<ll> vec;
for(int i=0 ; i<p ; ++i){
if(vec_ori[i]){
vec.push_back(i);
}
}
ll q = (N-1)/t;
ll r = (N-1)%t;
cout << p*q + vec[r] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define Tsetso ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pr pair < int , int >
const int N = 3e6+10,NN = 1005, length = 25 , mod = 1e9+7;
ll dp[4][N];
int main() {
Tsetso
ll n ,k ;
cin >> n >> k ;
dp[0][0] = 1;
for ( int i = 0 ; i < 3 ; i++) {
for (int j = 0; j <= i * n; j++) {
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + n + 1] -= dp[i][j];
}
for ( int j = 1 ; j <= (i+1) * n ; j++)
dp[i+1][j] += dp[i+1][j-1];
}
int sum ;
for ( int i = 3 ; i <= 3 * n ; i++)
{
if ( k <= dp[3][i])
{
sum = i ;
break;
}
k -= dp[3][i];
}
int x = 0;
for ( int i = max(1LL,sum - (2 * n)); i <= n ; i++)
{
if ( sum - i < 2)
break;
if (k <= dp[2][sum-i]) {
x = i;
break;
}
k -= dp[2][sum-i];
}
sum -= x ;
int y = max(1LL,sum -n) , z = sum - y;
y += k-1;
z -= k-1;
cout << x << ' ' << y << ' ' << z ;
} |
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#include<set>
#include <array>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound
#define si(x) int(x.size())
#define sum_all(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
const double pi = 2 * acos(0.0);
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };
const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 };
const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 };
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;
}
ll ceil1(ll a,ll b)
{
return(a+b-1)/b;
}
void read(vector<ll> & arr)
{
for(ll i=0;i<si(arr);i++)
cin >> arr[i];
}
void read_graph(vector<vector<ll>>& g, ll m)
{
while(m--)
{
ll x,y; cin >> x>> y ;
x--,y--;
g[x].pb(y);
g[y].pb(x);
}
}
void solve()
{
vector<int> arr(3);
for(int i=0;i<3;i++)
cin >> arr[i];
sort(arr.begin(),arr.end());
if(arr[1]-arr[0]==arr[2]-arr[1]) cout << "Yes";
else cout << "No";
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//int t; cin >> t; while (t--)
solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long mod=1e9+7;
const int maxn = 1e4+4;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int a[4];
void solve(){
int ok=0;
for(int i=1;i<=3;++i) a[i]=read();
for(int i=1;i<=3;++i){
for(int j=1;j<=3;++j){
for(int k=1;k<=3;++k){
if(i!=j&&j!=k&&i!=k&&(a[i]-a[j]==a[j]-a[k])) ok=1;
}
}
}
if(ok) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
int main (){
int t=1;
//t=read();
while(t--) solve();
} |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, a[51][3], b[51], x, y;
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
return (a.second > b.second);
}
void solve() {
cin >> n >> m >> q;
vector<pair<int, int> > t;
for(int i=1; i<=n; i++) {
cin >> a[i][1] >> a[i][2];
t.push_back(make_pair(a[i][1], a[i][2]));
}
sort(t.begin(), t.end(), sortbysec);
for(int i=1; i<=m; i++)
cin >> b[i];
for(int k=1; k<=q; k++) {
cin >> x >> y;
vector<int> v;
long long ans = 0;
for(int i=1; i<=m; i++) {
if(i<x || i>y) v.push_back(b[i]);
}
sort(v.begin(), v.end());
if(v.empty()) cout << 0 << '\n';
else {
for(auto z : t) {
for(int j=0; j<v.size(); j++) {
if(z.first <= v[j]) {
ans += z.second;
v.erase( v.begin() + j);
break;
}
}
}
cout << ans << '\n';
v.clear();
}
}
}
int main() {
solve();
return 0;
} | #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
#define Fast_IO ios::sync_with_stdio(false) ,cin.tie(0) ,cout.tie(0)
int main()
{
Fast_IO;
int n, k, x, y, res = 0;
cin >> n >> k;
for (int i=1; i<=n; i++)
{
cin >> x >> y;
res += (x*y);
if (res > k*100)
return cout << i, 0;
}
cout << -1;
return 0;
}
|
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
inline int gi(){
char tmp=getchar();int ans=0;
int flag=1;
while(!isdigit(tmp)){
if(tmp=='-'){
tmp=getchar();
flag=-1;
break;
}
tmp=getchar();
}
while(isdigit(tmp)){
ans=ans*10+tmp-'0';
tmp=getchar();
}
return ans*flag;
}
const int N = 201;
int S[N],R[N],F[N],Mark[N],L[N];
inline int Check(int l,int r){ // check if [l,r] is a "loop section".
int range = ((l+r)>>1)-l+1;
for(int i=l;i<l+range;++i){
if(R[S[i]]) return 0;
if(S[i]==0){
if(R[S[i+range]])
continue;
if(!S[i+range])
continue;
}
if(S[i]){
if(L[S[i]])
if(S[i+range]==0 || S[i+range] == S[i])
continue;
if(Mark[S[i]])
if(S[i+range]==S[i])
continue;
}
return 0;
}
return 1;
}
signed main()
{
#ifdef TSUKIAKIOI
freopen("data.in","r",stdin);
#endif
int n=gi();
for(int i=1;i<=n;++i){
int l,r;l=gi(),r=gi();
if(S[l] || S[r]) return puts("No"),0;
if(l>r && r!=-1) return puts("No"),0;
if(~l&&~r){
S[l]=i;S[r]=i;Mark[i]=1;
continue;
}
if(l==-1 && r==-1)
continue;
if(l==-1){
if(S[r]) return puts("No"),0;
S[r]=i;R[i]=1;
}
if(r==-1){
if(S[l]) return puts("No"),0;
S[l]=i;L[i]=1;
}
}
F[0]=1;
Check(5,6);
for(int i=2;i<=n<<1;i+=2){
for(int j=0;j<i && !F[i];j+=2){
if(!F[j]) continue;
if(Check(j+1,i)){
F[i] |= F[j];
}
}
}
if(F[n<<1])
return puts("Yes"),0;
return puts("No"),0;
}
| #include<bits/stdc++.h>
using namespace std;
#include <numeric>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define int long long int
#define MOD 1000000007
#define INT_BITS 16
#define INF 1e18
#define pi 3.141592
#define watch(x) cout<<(#x)<<"="<<(x)<<"\n"
#define pb push_back
#define se second
#define fi first
#define pii pair<int,,int>
#define MAXN 1001
#define all(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define deb_arr(a,n) {cout<<#a<<":";for(int i=0;i<n;i++) {cout<<a[i]<<" ";} cout<<'\n';}
#define deb_mat(dist,n,m) {cout<<#dist<<"\n";for(int i=0;i<n;i++){for(int j=0;j<m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}}
#else
#define deb(x) ;
#define debug(x,y) ;
#define deb_arr(a,n) ;
#define deb_mat(dist,n,m) ;
#endif
//__builtin_popcount
int dx[] = { 1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};
//=========================================================================================
int fact[300000], numinv[300000], factinv[300000];
int find_nCr(int n, int r)
{
int ans = fact[n];
ans = (ans * factinv[r]) % MOD;
ans = (ans * factinv[n - r]) % MOD;
return ans;
}
void factorial()
{
fact[0] = 1;
for (int i = 1; i < 300000; i++)
{
fact[i] = (fact[i - 1] * i) % MOD;
}
numinv[0] = 1;
numinv[1] = 1;
for (int i = 2; i < 300000; i++)
numinv[i] = (MOD - (MOD / i) * numinv[MOD % i] % MOD) % MOD;
factinv[0] = factinv[1] = 1;
for (int i = 2; i < 300000; i++)
factinv[i] = (numinv[i] * factinv[i - 1]) % MOD;
}
//=======================================================================================
const int N = 200004;
int incoming[N];
vector<int>v[N];
int n,m;
vector<int>temp;
int a[N];
int b[N];
void topo()
{
for(int i=0;i<n;i++)
{
if(incoming[i]==0)
{
//q.push(i);
temp.push_back(i);
}
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
cin>>n>>m;
for(int i=0;i<n;i++)
{
b[i]=INF;
cin>>a[i];
}
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
x--;
y--;
v[x].pb(y);
incoming[y]++;
}
topo();
set<int>q;
int vis[n]={0};
for(auto it:temp)
{
b[it]=INF;
q.insert(it);
vis[it]=1;
//watch(it);
}
int ans=INT_MIN;
while(!q.empty())
{
int x = *(q.begin());
q.erase(q.begin());
//vis[x]=0;
for(auto it:v[x])
{
ans=max(a[it]-min(a[x],b[x]),ans);
b[it]=min(min(a[x],b[x]),b[it]);
if(vis[it]==0)
q.insert(it);
vis[it]=1;
}
}
cout<<ans<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repn(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define vi vector<ll>
#define vii vector<pii>
#define vvi vector<vector<int>>
#define mi map<int,int>
#define mii map<pii,int>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
// #define mp make_pair
#define sz(x) (int)x.size()
#define all(v) (v).begin(), (v).end()
#define ret(x) return cout<<x,0;
#define rety return cout<<"YES",0;
#define retn return cout<<"NO",0;
#define fl fflush(stdout)
#define hell 1000000007
#define hell2 998244353
#define pi 3.14159265358979323846
int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=ans*a%m; b/=2;a=a*a%m;}return ans;}
int inverse(int a, int p){return fexp(a, p-2,p);}
int fac[300005]; //fac[0]=0;fac[1]=1;rep(i,2,300005)fac[i]=(fac[i-1]+fac[i-2])%hell;
int ncr(int n, int r,int p) {
if (r==0) return 1;
return (fac[n]*inverse(fac[r],p)%p*inverse(fac[n-r],p)%p)%p;
}
void code_hunter(){
ll n,m;cin>>n>>m;
cout<<n/m;
}
signed main(){
IOS;
int t=1;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
// cin>>t;
while(t--){
code_hunter();
// cout<<"\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using vi = vector<int>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1e9+10;
constexpr long long LINF = 1e18+10;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct edge{
int from;
int to;
int c;
};
int main(){
int n, m; cin >> n >> m;
vector<vector<edge>> G(n+10);
rep(i, m){
int u, v, c; cin >> u >> v >> c;
edge e1{u,v,c};
edge e2{v,u,c};
G[u].push_back(e1);
G[v].push_back(e2);
}
//始点=頂点0としてBFS
vector<int> ans(n+10, INF);//頂点に振る番号
queue<pii> que; //頂点番号,ラベル
que.push({1, INF});//-1
while(!que.empty()){
auto p = que.front(); que.pop();
int v = p.first, c = p.second; //一致なら正、不一致なら負
//cout << "v:" << v << " c:" << c << ln;
if(ans[v]!=INF)continue;
if(c==INF)ans[v] = 1;
else{
if(c>0){
ans[v] = (c == 1 ? 2 : 1);
}else{
c = -c;
ans[v] = c;
}
}
for(auto e: G[v]){
if(ans[e.to]!=INF)continue;
int nc = e.c;
if(ans[v]!=nc) nc = -e.c;
que.push({e.to, nc});
}
}
for(int i=1; i<=n; i++){
cout << ans[i] << ln;
}
}
|
#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 si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define M 1000000007
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ii> vii;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1, i;
// cin >> t;
while (t--) {
int n,count=0;
cin>>n;
pair<int,int> p[n];
fo(i, n)
{
cin>>p[i].first>>p[i].second;
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int temp=0,temp2=0;
temp=p[j].second-p[i].second;
temp=abs(temp);
temp2=p[j].first-p[i].first;
temp2=abs(temp2);
if(temp<=temp2)
{
count++;
}
}
}
cout<<count;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long double k=0.6180339887498948482045;
ll x,y,tmp;
vector<int> ans;
int main(){
scanf("%lld",&tmp);if(tmp==0) {printf("0\n");return 0;}
//printf("%lf\n",((long double)tmp)*k-50);
for(ll i=max((ll)(((long double)tmp)*k)-50,0ll);;i++){
x=tmp;y=i;ans.clear();
while(x&&y){
if(x>y) ans.push_back(3),x=x-y;
else ans.push_back(4),y=y-x;
if(ans.size()>130) {break;}
}
if(ans.size()+max(x,y)<=130){
if(x) for(int i=1;i<=x;i++) ans.push_back(1);
else for(int i=1;i<=y;i++) ans.push_back(2);
printf("%d\n",ans.size());
for(int i=ans.size()-1;i>=0;i--) printf("%d\n",ans[i]);
return 0;
}
}
return 0;
}/*
47584759575744849
*/ |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fr(i,n) for(int i=0;i<n;i++)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
int j=t*1.08;
if(j>206)
cout<<":("<<endl;
else if(j==206)
cout<<"so-so"<<endl;
else
cout<<"Yay!"<<endl;
return 0;
} | // You told me...
// At your absolute best, you still won't be good enough for the wrong person...
// At your worst, you'll still be worth it to the right person...
// ............................................................................................
// It was good while it lasted, good bye... .
// I believe I really loved you... to that point that I always want to hear your voice... .
// But before my hand could reach you... you seem to be slowly disappearing from my sight... .
// ............................................................................................
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define fill(x, v) memset(x, v, sizeof(x))
#define all(v) (v).begin(), (v).end()
#define FER(i,a,b) for(ll i=ll(a); i< ll(b); ++i)
#define IFR(i,a,b) for(ll i=ll(a); i>=ll(b); --i )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
// #define N 680005
// #define mod1 1000000007
#define mod1 998244353
// #define mod2 1000000009
#define bas 987625403
#define sqr(x) (x)*(x)
#define INF 2e18
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii > tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template<typename t> void fff(const char* x, t&& val1) { cout<<x<< " : "<<val1<<"\n";}
template<typename t1, typename... t2> void fff(const char* x, t1&& val1, t2&&... val2){
const char* xd=strchr(x+1, ',');
cout.write(x, xd-x)<<" : "<<val1<<" | ";
fff(xd+1, val2...);
}
inline ll add(ll a, ll b, ll mod) { return a+b < mod? a+b : a+b-mod;}
inline ll rem(ll a, ll b, ll mod) { return a >= b? a - b: a- b + mod;}
inline ll mul(ll a, ll b, ll mod) { return (int) a*b%mod;}
inline void Mul(ll &a, ll b, ll mod) { a = (int) a*b%mod;}
inline ll bp(ll a, ll p, ll mod){
ll ret;
for(ret = 1; p; p>>=1, Mul(a, a, mod)) (p & 1) && (Mul(ret, a, mod), 1);
return ret;
}
static inline void read(ll &result) {
bool minus = false;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' and ch <= '9') break;
ch = getchar();
}
(ch == '-')? minus = true: result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' or ch > '9') break;
result = (result<<3)+ (result<<1) + (ch - '0');
}
if(minus) result = -result;
}
int main(){
ll n; read(n);
double total = n * 1.08;
if((ll) total < 206) cout<<"Yay!\n";
else if((ll) total == 206) cout<<"so-so\n";
else cout<<":(\n";
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll cnt[10];
string s, t;
ll occ[10][2];
ll p10[10];
bool check(){
ll sum1 = 0, sum2 = 0;
for (ll i = 1; i <= 9; i++) sum1 += p10[occ[i][0]] * i;
for (ll i = 1; i <= 9; i++) sum2 += p10[occ[i][1]] * i;
return (sum1 > sum2);
}
int main(){
cout << fixed << setprecision(12);
fastio;
p10[0] = 1;
for (ll i = 1; i < 10; i++) p10[i] = p10[i - 1] * 10;
ll k; cin >> k;
for (ll i = 1; i <= 9; i++) cnt[i] = k;
cin >> s >> t;
for (ll i = 0; i < 4; i++){
cnt[s[i] - '0']--;
cnt[t[i] - '0']--;
occ[s[i] - '0'][0]++;
occ[t[i] - '0'][1]++;
}
ll sum_choice = 0, sum_good = 0;
for (ll u = 1; u <= 9; u++){
for (ll d = 1; d <= 9; d++){
ll choice;
if (u == d) choice = cnt[u] * (cnt[u] - 1);
else choice = cnt[u] * cnt[d];
sum_choice += choice;
occ[u][0]++; occ[d][1]++;
if (check()) sum_good += choice;
occ[u][0]--; occ[d][1]--;
}
}
ld tmp = sum_good; tmp /= sum_choice;
cout << tmp << "\n";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = b - 1; a <= i; i--)
ll MOD = 1000000007;
ll K, cnt_a[10], cnt_t[10], ao, taka, win, total;
string T, A;
int main () {
cin >> K >> T >> A;
rep (i, 0, 4) {
cnt_t[T[i] - '0']++;
cnt_a[A[i] - '0']++;
}
rep (ura_a, 1, 10) rep (ura_t, 1, 10) {
cnt_a[ura_a]++;
cnt_t[ura_t]++;
if (cnt_a[ura_a] + cnt_t[ura_a] <= K && cnt_a[ura_t] + cnt_t[ura_t] <= K) {
ll d;
if (ura_a == ura_t) {
d = (K - cnt_a[ura_a] - cnt_t[ura_t] + 2) * (K - cnt_a[ura_a] - cnt_t[ura_t] + 1);
} else {
d = (K - cnt_a[ura_a] - cnt_t[ura_a] + 1) * (K - cnt_a[ura_t] - cnt_t[ura_t] + 1);
}
ao = 0, taka = 0;
rep (i, 1, 10) {
ll x = i;
rep (j, 0, cnt_a[i]) x *= 10;
ao += x;
x = i;
rep (j, 0, cnt_t[i]) x *= 10;
taka += x;
}
total += d;
if (taka > ao) win += d;
}
cnt_a[ura_a]--;
cnt_t[ura_t]--;
}
double ans = (double)win / total;
cout << fixed << setprecision(7) << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define MOD 1000000007
int main(void) {
int m,h;
cin >> m >> h;
if(h%m==0) cout << "Yes" << endl;
else cout << "No" << endl;
} | #include <iostream>
#include <algorithm>
using namespace std;
int main(){
int m, h;
cin >> m >> h;
if (h % m == 0)
cout << "Yes\n";
else
cout << "No\n";
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int H,W;
cin >> H >> W;
int min;
vector<vector<int>> data(H,vector<int>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> data.at(i).at(j);
if (i == 0 && j == 0) {
min = data.at(i).at(j);
}
else if (data.at(i).at(j) < min) {
min = data.at(i).at(j);
}
}
}
long long sum = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (data.at(i).at(j) > min) {
sum += data.at(i).at(j) - min;
}
}
}
cout << sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
ll sov(ll b,ll c){
if(c==1){
if (b==0) return 1;
return 2;
}
if (b==0) return c;
if (b>(c/2)) return c*2-1;
if (b<=-(c/2)) return c*2-1;
if (b>0) return c+2*b-1;
if (b<0) return c-2*b;
}
int main() {
ll b,c,ans;
cin >> b >> c;
cout << sov(b,c) << endl;
return 0;
} |
#include <iostream>
#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>
// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>
typedef long long LL;
//typedef long double LD;
//typedef pair<LL, LL> P;
using namespace std;
//using Graph = vector<vector<LL>>;
const LL MOD=1000000007;
//const LL MOD=998244353;
//const LL MAX=100100;
//const LL NIL=-1;
//const LL INF=2000000000000000000;
//const LL SEG_VAL = 1 << 19;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
LL t;
LL n;
cin >> t >> n;
vector<pair<LL, LL>> vr;
LL cur=1;
LL i=0;
while(vr.size()<11){
if((i*t)/100>=cur){
vr.push_back(make_pair(i, (i*t)%100));
cur*=2;
}
i++;
}
LL num=0;
LL res=0;
/*
for(LL i=0; i<11; i++){
cout << vr[i].first << " " << vr[i].second << endl;
}
*/
while(n>0){
//cout << n << " " << num << " " << res << endl;
if(n<2048){
while(n>0){
num++;
res+=t;
if(res/100>=1){
res-=100;
n--;
//cout << num << " " << res << endl;
}
}
}else{
num+=vr[10].first;
n-=1024;
n-=(res+vr[10].second)/100;
res=(res+vr[10].second)%100;
}
}
cout << num+(num*t/100)-1 << endl;
return 0;
}
| #include <iostream>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>
// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>
typedef long long LL;
//typedef long double LD;
//typedef pair<LL, LL> P;
using namespace std;
//using Graph = vector<vector<LL>>;
//const LL MOD=1000000007;
//const LL MOD=998244353;
//const LL MAX=100100;
//const LL NIL=-1;
//const LL INF=2000000000000000000;
//const LL SEG_VAL = 1 << 19;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
LL n;
LL k;
cin >> n >> k;
LL ans=0;
for(LL i=1; i<=n; i++){
for(LL j=1; j<=k; j++){
ans+=100*i+j;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define unvisited -1
#define visited 1
#define eps 1e-9
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define uint64 unsigned long long
#define FastSlowInput ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define debug if(true)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int main(){
int n,k,m,i,j;
scanf("%d %d %d",&n,&k,&m);
int a[101];
int sum = 0;
for(i=0;i<n-1;i++){
scanf("%d",&a[i]);
sum += a[i];
}
int tot = n*m;
int x = tot - sum;
if(x < 0){
x = 0;
}
if(x >= 0 && x <= k){
printf("%d\n",x);
}else{
printf("-1\n");
}
return 0;
}; | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ln "\n"
#define si(n) scanf("%d", &n);
#define pi(n) printf("%d\n", n);
#define sd(n) scanf("%lf", &n);
#define pd(n) printf("%.2lf\n", n);
const double EPS = (double) 1e-9;
const double pii = acos(-1);
const int mod = 1000000007;
const int N = (int) 1e5;
int main()
{
fast_in_out;
int a, b, x, y;
cin >> a >> b >> x >> y ;
int ans = x;
if(a == b)
{
cout << ans << ln;
}
else if(a < b)
{
int dif = b - a;
int cnt1 = dif * y;
int cnt2 = dif * x * 2;
ans += min(cnt1, cnt2);
cout << ans << ln;
}
else
{
int dif = a - b;
if(dif == 1)
{
cout << ans << ln;
}
else
{
dif--;
int cnt1 = dif * y;
int cnt2 = dif * x * 2;
ans += min(cnt1, cnt2);
cout << ans << ln;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 2e5+2;
int n;
int a[MAX_N];
int b[MAX_N];
int p[MAX_N];
vector<int> g[MAX_N];
void solve()
{
if(n == 1){
cout<<0<<'\n';
return;
}
for(int i=1;i<=n;++i)if(p[i]!=i && a[i] <= b[p[i]]){
cout<<-1<<'\n';
return;
}
vector<pair<int,int>> ans;
for(int i=1;i<=n;++i)if(a[i]!=-1){
vector<int> cycle;
int mx=-1,s=-1,v=i;
while(a[v]>=0){
if(a[v]>mx){
mx=a[v];
s=(int)cycle.size();
}
cycle.push_back(v);
a[v]=-1;
v=p[v];
}
rotate(cycle.begin(),cycle.begin()+s,cycle.end());
for(int i=1;i<(int)cycle.size();++i){
ans.emplace_back(cycle[0],cycle[i]);
}
}
cout<<ans.size()<<'\n';
for(auto& x : ans) cout<<x.first<<' '<<x.second<<'\n';
}
int main()
{
cin >> n;
for(int i=1;i<=n;++i) cin >> a[i];
for(int i=1;i<=n;++i) cin >> b[i];
for(int i=1;i<=n;++i) cin >> p[i];
solve();
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5+8, inf = 1e18+9, mod = 1e9+7;
int n, m, a[maxn], b[maxn], p[maxn], vs[maxn];
void solve() {
int i, j;
//cin >> n;
scanf("%lld", &n);
vector<pair<int, int>> vec;
for (i = 1; i <= n; i++) scanf("%lld", &a[i]); //cin >> a[i];
for (i = 1; i <= n; i++) scanf("%lld", &b[i]); //cin >> b[i];
for (i = 1; i <= n; i++) scanf("%lld", &p[i]); //cin >> p[i];
for (i = 1; i <= n; i++) if (a[i] <= b[p[i]] && i != p[i]) return (void)(printf("-1\n"));
for (i = 1; i <= n; i++) if (!vs[i]) {
int start = i; vs[i] = 1;
if (i == p[i]) continue;
for (j = p[i]; j != i; j = p[j]) {
vs[j] = 1;
if (a[j] > a[start]) start = j;
}
for (j = p[start]; j != start; j = p[j]) vec.push_back({start, j});
}
//cout << vec.size() << endl;
printf("%lld\n", (int)vec.size());
for (auto [x, y] : vec) printf("%lld %lld\n", x, y); //cout << x << " " << y << endl;
}
signed main() {
//ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#define N 201000
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 * 10 + (c ^ 48), c = getchar();
if (flag) x = -x;
}
using namespace std;
int n;
int a[N], b[N], h[N], htot;
long long ans;
inline void sol(int L, int R) {
if (L + 1 == R) return ;
// printf("sol(%d, %d) : \n", L, R);
int ptr = R;
for (int i = L + 1; i < R; ++i) if (b[i] > a[i]) { ptr = i; break; }
// printf("ptr = %d\n", ptr);
int tol = a[ptr - 1];
for (int i = ptr - 1; i > L; --i) {
if (b[i] < tol) {
int p = upper_bound(a + L, a + R + 1, b[i]) - a - 1;//bug
// printf("i = %d, p = %d\n", i, p);
ans += i - p;
tol = b[i];
}
}
int tor = a[ptr];
for (int i = ptr; i < R; ++i) {
if (b[i] > tor) {
int p = lower_bound(a + L, a + R + 1, b[i]) - a;//bug bug
// printf("i = %d, p = %d\n", i, p);
ans += p - i;
tor = b[i];
}
}
}
int main() {
read(n);
int L;
read(L);
++n; ++n;
a[0] = b[0] = 0; a[n - 1] = b[n - 1] = L + 2 - n;//0 ... L + 1
for (int i = 1; i < n - 1; ++i) read(a[i]), a[i] -= i;
for (int i = 1; i < n - 1; ++i) read(b[i]), b[i] -= i;
// for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
// for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
for (int i = 0; i < n; ++i) h[++htot] = a[i];
htot = unique(h + 1, h + 1 + htot) - h - 1; h[htot + 1] = -12341;
for (int i = 0; i < n; ++i) a[i] = lower_bound(h + 1, h + 1 + htot, a[i]) - h;
for (int i = 0; i < n; ++i) {
int v = lower_bound(h + 1, h + 1 + htot, b[i]) - h;
if (h[v] != b[i]) return puts("-1"), 0;
b[i] = v;
}
// for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
// for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
int lst = 0;
for (int i = 1; i < n; ++i) if (a[i] == b[i]) {
// printf("\ni = %d : \n", i);
sol(lst, i); lst = i;
// printf("ans = %lld\n", ans);
}
printf("%lld\n", ans);
return 0;
}
| #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
using namespace std;
int n,q;
ll a[100010],c[100010];
ll k;
int main(){
cin >> n >> q;
rep(i,n){
cin >> a[i];
}
sort(a, a+n);
rep(i,n){
c[i] = a[i]-i-1;
}
rep(ii,q){
cin >> k;
int ldx = lower_bound(c, c+n, k)-c;
if(ldx >= 0 && ldx < n && c[ldx] == k){
cout << a[ldx]-1 << endl;
continue;
}
int idx = upper_bound(c, c+n, k)-c-1;
// cout << "ii " << ii << " idx " << idx <<endl;
if(idx == -1){
cout << k << endl;
}
else {
cout << a[idx]+k-c[idx]<<endl;
}
}
} |
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
int day;
cin >> day;
if (day % 2 == 0) {
cout << "White";
}
if (day % 2 == 1) {
cout << "Black";
}
return 0;
} | #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;
int N;
int main(){
cin>>N;
if(N&1) cout<<"Black"<<endl;
else cout<<"White"<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n; ll x; cin >> n >> x;
vector<ll> A(n); rep(i, n) cin >> A[i];
vector<ll> cnt(n);
{
ll rem = x;
rep(i, n) {
if(i == n-1) cnt[i] = rem / A[i];
else cnt[i] = (x % A[i+1]) / A[i];
}
}
vector<vector<ll>> dp(n, vector<ll>(2));
dp[0][0] = 1;
rep(i, n-1) {
if(cnt[i]) {
dp[i+1][0] += dp[i][0];
dp[i+1][1] += dp[i][0];
} else {
dp[i+1][0] += dp[i][0];
}
if(cnt[i]+1 < A[i+1]/A[i]) {
dp[i+1][0] += dp[i][1];
dp[i+1][1] += dp[i][1];
} else {
dp[i+1][1] += dp[i][1];
}
}
cout << dp[n-1][0] + dp[n-1][1] << endk;
return 0;
}
| #include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using ll = long long;
using namespace std;
ll n, x;
ll a[50];
ll v[50];
ll l[50];
ll dp[51];
ll dq[51];
int main() {
cin >> n >> x;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) l[i - 1] = a[i] / a[i - 1] - 1;
l[n - 1] = 2e18;
ll s = x;
for (int i = n - 1; i >= 0; --i) {
v[i] = s / a[i];
s %= a[i];
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
if (v[i] == 0) {
dp[i + 1] = dp[i] + dq[i];
dq[i + 1] = dq[i];
} else {
dp[i + 1] = dp[i] + (v[i] < l[i]) * dq[i];
dq[i + 1] = dp[i] + dq[i];
}
}
cout << dp[n] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;
#define pb push_back
#define ff first
#define ss second
#define MAX 1000005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };
void add_edge(int a, int b, int c, int d, int n)
{
int u=(a-1)*n+b,v=(c-1)*n+d;
adj[u].pb(v);
adj[v].pb(u);
}
int dist[MAX];
void bfs(int s)
{
memset(dist,-1,sizeof dist);
queue<int> q;
q.push(s);
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v: adj[u])
{
if(dist[v]==-1)
{
dist[v]=dist[u]+1;
q.push(v);
}
}
}
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.txt","w",stdout);
cin.tie(0),cout.tie(0);
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
int TC=1;
int n,m,k,q;
//cin>>TC;
for(int t1=1; t1<=TC; t1++)
{
cin>>n>>m;
vector<aiii> edges(m);
int mp[n*n+1];
memset(mp,-1,sizeof mp);
for(auto &[u,v,c]: edges)
{
char c1;
cin>>u>>v>>c1;
c=c1;
mp[(u-1)*n+v]=1;
mp[(v-1)*n+u]=1;
}
for(auto [a,b,c1]: edges)
{
for(auto [c,d,c2]: edges)
{
if(c2==c1)
{
add_edge(a,c,b,d,n);
add_edge(c,a,d,b,n);
add_edge(a,d,b,c,n);
add_edge(d,a,c,b,n);
}
}
}
bfs(n);
int mn=INF;
for(int i=1;i<=n*n;i++)
{
if(dist[i]!=-1)
{
int r=i%n;
int d=(i-r)/n;
if(d+1==r)
mn=min(mn,2*dist[i]);
else if(mp[i]==1)
mn=min(mn,2*dist[i]+1);
}
}
cout<<(mn==INF?-1:mn)<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}
const char newl = '\n';
template <typename T>
void dijkstra(int s,vector<vector<pair<int,T>>>& edges,vector<T>& dist) {
priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
dist[s] = 0;
que.push(make_pair(0,s));
while (!que.empty()) {
pair<ll,int> p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (pair<int,T>& e : edges[v]) {
if (dist[e.first] > dist[v]+e.second) {
dist[e.first] = dist[v]+e.second;
que.push(make_pair(dist[e.first],e.first));
}
}
}
}
int main() {
int n,m;
cin >> n >> m;
vector<P> a[26];
for (int i = 0;i < m;++i) {
int u,v;char c;
cin >> u >> v >> c;
a[c-'a'].push_back(make_pair(u-1,v-1));
}
vector<vector<P>> edges(n*n);
for (int c = 0;c < 26;++c) {
for (int i = 0;i < a[c].size();++i) for (int j = 0;j < a[c].size();++j) {
P &p = a[c][i];
P &q = a[c][j];
edges[p.first*n+q.first].push_back(make_pair(p.second*n+q.second,1));
edges[p.second*n+q.second].push_back(make_pair(p.first*n+q.first,1));
edges[p.second*n+q.first].push_back(make_pair(p.first*n+q.second,1));
edges[p.first*n+q.second].push_back(make_pair(p.second*n+q.first,1));
}
}
vector<int> dist(n*n,1e9);
dijkstra<int>(n-1,edges,dist);
cout << (dist[(n-1)*n] < 1e9 ? dist[(n-1)*n] : -1) << newl;
} |
#include<bits/stdc++.h>
#define mxn 100005
#define inf 1e9
using namespace std;
int n, mem[mxn][26];
char str[mxn];
int dp(int pos, int ch)
{
if(pos==n)return inf;
if(pos==n-1 && (str[n-1]-'a')!=ch)return 1;
int &ret= mem[pos][ch];
if(ret!=-1)return ret;
ret= dp(pos+1, ch);
if((str[pos]-'a')!=ch)ret= min(ret, 1+dp(pos+2, str[pos+1]-'a'));
return ret;
}
int main()
{
scanf("%d %s", &n, str);
memset(mem, -1, sizeof mem);
int res= dp(1, str[0]-'a');
if(res==inf)res= -1;
printf("%d\n", res);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template<typename T, typename U>
ostream& operator<<(ostream& o, const map<T,U>& m) {
o << "{";
for (const pair<T,U>& p : m)
o << p.first << ": " << p.second << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const set<T>& s) {
o << "{";
for (const T& e : s)
o << s << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
for (const T& x : v)
o << x << ",";
return o << "]";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T>>& v) {
o << "[";
for (const vector<T>& x : v)
o << x << ",\n";
return o << "]";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
if (s[0] != s[n-1]) {
cout << "1\n";
return 0;
}
for (int i=1; i<n-2; ++i) {
if (s[0] != s[i] && s[i+1] != s[n-1]) {
cout << "2\n";
return 0;
}
}
cout << "-1\n";
}
|
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,l,m,n,o,p;
cin>>a>>b;
cin>>c>>d;
l=a-c;
m=b-c;
n=a-d;
o=b-d;
int max=l;
if(m>max){
max=m;
}
if(n>max){
max=n;
}
if(o>max){
max=o;
}
cout<<max;
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;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset_find(k) find_by_order(k)
#define oset_order(k) order_of_key(k)*/
using ll = long long;
using db = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define ROF(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
constexpr int pct(int x) { return __builtin_popcount(x); }
template<typename T> bool fmin(T& a, const T& b) { return b<a?a=b,1:0; }
template<typename T> bool fmax(T& a, const T& b) { return a<b?a=b,1:0; }
#define ts to_string
string ts(char c) { return '\'' + string(1, c) + '\''; }
string ts(const char* s) { return (string)s; }
string ts(const string& s) { return '"' + s + '"'; }
string ts(bool b) { return (b ? "true" : "false"); }
string ts(vector<bool> v) {
string res = "{";
FOR(i, 0, sz(v))
res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ> string ts(bitset<SZ> b) {
string res = "";
FOR(i, 0, SZ)
res+=char('0'+b[i]);
return res;
}
template <typename A, typename B> string ts(pair<A, B> p);
template <typename A> string ts(A v) {
bool first = 1; string res = "{";
for (const auto& x: v) {
if (!first) res += ", ";
first = 0; res += ts(x);
}
res += "}"; return res;
}
template <typename A, typename B> string ts(pair<A, B> p) {
return "(" + ts(p.fi) + ", " + ts(p.se) + ")";
}
void dbg_out() { cerr << "]" << endl; }
template <typename H, typename... T> void dbg_out(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
dbg_out(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//const ll MOD = 1000000007; //998244353;
//const ll INF = LLONG_MAX;
//const db PI = 3.14159265358979323846;
ll n;
ll a[200005];
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
cout << b - c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while (tc--)
solve();
} |
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
using namespace std;
int n,i,j,k,l,tot;
int ans[200001];
int a[200001];
bool bz[200001];
void swap(int &x,int &y) {int z=x;x=y;y=z;}
void Exit()
{
printf("-1\n");
exit(0);
}
int main()
{
#ifdef file
freopen("c.in","r",stdin);
#endif
scanf("%d",&n);
fo(i,1,n) scanf("%d",&a[i]);
l=1;
fo(i,1,n)
if (a[i]==l && i>l)
{
fd(j,i-1,l)
if (!bz[j])
bz[j]=1,swap(a[j],a[j+1]),ans[++tot]=j;
else
Exit();
while (l<=n && a[l]==l) ++l;
if (l>n) break;
}
fo(i,1,n-1) if (a[i]>a[i+1]) Exit();
if (tot<n-1) Exit();
else
{
fo(i,1,n-1)
printf("%d\n",ans[i]);
}
fclose(stdin);
fclose(stdout);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(4e5)+10;
int n,fa[maxn],dis[maxn],dep[maxn];
vector<int> g[maxn];
int lg[maxn];
int mx,son[maxn];
void dfs(int u,int p) {
dis[u]=dis[p]+1; fa[u]=p;
for (int v : g[u]) if (v!=p) dfs(v,u);
if (!mx||dis[u]>dis[mx]) mx=u;
}
int st[maxn][20];
int dfn[maxn],tot,idx,euler[maxn],pos[maxn],ans[maxn];
void dfs2(int u,int p) {
dfn[++idx]=u;
for (int v : g[u]) if (v!=p&&v!=son[u]) dfs2(v,u);
if (son[u]) dfs2(son[u],u);
}
int MN(int x,int y) {
if (dep[x]<dep[y]) return x;
return y;
}
int lca(int x,int y) {
x=pos[x],y=pos[y];
if (x>y) swap(x,y);
int j=lg[y-x+1];
return MN(st[x][j],st[y-(1<<j)+1][j]);
}
int DIS(int x,int y) {
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
void pre(int u,int p) {
dep[u]=dep[p]+1;
euler[++tot]=u; pos[u]=tot;
for (int v : g[u]) if (v!=p) {
pre(v,u);
euler[++tot]=u;
}
}
int main() {
//freopen("1.txt","r",stdin);
for (int i=2;i<maxn;i++) lg[i]=lg[i>>1]+1;
read(n); int x,y,z;
for (int i=1;i<n;i++) {
read(x),read(y);
g[x].push_back(y);
g[y].push_back(x);
}
pre(1,0);
for (int i=1;i<=tot;i++) st[i][0]=euler[i];
for (int j=1;j<=19;j++)
for (int i=1;i+(1<<j)-1<=tot;i++)
st[i][j]=MN(st[i][j-1],st[i+(1<<(j-1))][j-1]);
mx=0; dfs(1,0); x=mx;
mx=0; dfs(x,0); y=mx;
z=y;
while (fa[z]) son[fa[z]]=z,z=fa[z];
dfs2(x,0);
ans[x]=1;
for (int i=2;i<=n;i++)
ans[dfn[i]]=ans[dfn[i-1]]+DIS(dfn[i],dfn[i-1]);
for (int i=1;i<=n;i++) printf("%d ",ans[i]); puts("");
return 0;
}
/*
REMEMBER:
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/ |
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repn(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define vvi vector<vector<int>>
#define mi map<int,int>
#define mii map<pii,int>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sz(x) (int)x.size()
#define all(v) (v).begin(), (v).end()
#define ret(x) return cout<<x,0;
#define rety return cout<<"Yes",0;
#define retn return cout<<"No",0;
#define fl fflush(stdout)
#define hell 1000000007
#define hell2 998244353
#define pi 3.14159265358979323846
#define piii pair<pii,int>
int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=ans*a%m; b/=2;a=a*a%m;}return ans;}
int inverse(int a, int p){return fexp(a, p-2,p);}
int fac[300005]; //fac[0]=0;fac[1]=1;rep(i,2,300005)fac[i]=(fac[i-1]*i)%hell;
int ncr(int n, int r,int p) {
if (r==0) return 1;
return (fac[n]*inverse(fac[r],p)%p*inverse(fac[n-r],p)%p)%p;
}
bool sortby(piii &a,piii &b){
if(a.ff.ff==b.ff.ff)return (a.ff.ss<b.ff.ss);
return (a.ff.ff<b.ff.ff);
if(a.ss==b.ss)return (a.ff<b.ff);
return (a.ss<b.ss);
}
int solve(){
int n,m,a;
cin>>n>>m;
// if(m==0)ret(1);
vi v;
v.pb(0);
rep(i,0,m){
cin>>a;
v.pb(a);
}
sort(all(v));
v.pb(n+1);
int g=1e18;
rep(i,1,sz(v)){
//cout<<v[i]-v[i-1]-1<<" ";
if(v[i]!=v[i-1]+1)
g=min(g,v[i]-v[i-1]-1);
}
int ans=0;//ret(g);
rep(i,1,sz(v)){
ans+=((v[i]-v[i-1]-1+g-1)/g);
}
cout<<ans;
return 0;
}
signed main(){
auto start = high_resolution_clock::now();
IOS;
int t=1;
//cin>>t;
while(t--){
solve();
cout<<"\n";
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cerr<<"Time taken:"<<duration.count()/1000000.0<<"seconds"<<"\n";
return 0;
} |
/*
/> フ
| _ _|
/`ミ _x 彡
/ |
/ ヽ ?
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
*/
#pragma GCC optimize(2)
#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;
template <typename T>
void read(T &x) {
x = 0; bool f = 0;
char c = getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
for (;isdigit(c);c=getchar()) x=x*10+(c^48);
if (f) x=-x;
}
template<typename F>
inline void write(F x, char ed = '\n') {
static short st[30];short tp=0;
if(x<0) putchar('-'),x=-x;
do st[++tp]=x%10,x/=10; while(x);
while(tp) putchar('0'|st[tp--]);
putchar(ed);
}
template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }
template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }
const int N = 102;
int n, k, P, ans[N], lim;
int f[N][N*N*N/2], sum[N*N*N/2];
inline void add(int &x, int y) { x += y, x >= P && (x -= P); }
void work(void) {
f[0][0] = 1;
for (int i = 1;i <= n; i++) {
lim += i * k;
for (int j = 0;j <= lim; j++)
sum[j] = (f[i-1][j] + (j >= i ? sum[j-i] : 0)) % P;
for (int j = lim;j >= 0; j--)
f[i][j] = sum[j], j >= (k + 1) * i && (add(f[i][j], P - sum[j - (k + 1) * i]), 0);
}
}
int main() {
read(n), read(k), read(P), work();
for (int i = 1;i <= n; i++) {
ll ans = k;
for (int t = 1;t <= lim; t++)
ans = (ans + (ll)(k + 1) * f[i-1][t] % P * f[n-i][t]) % P;
write(ans);
}
return 0;
}
/*
100 100 998244353
*/ |
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int) x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
using ll = long long;
using ld = long double;
using namespace std;
int n, k, MOD = 998244353;
ll res;
int f(int x) {
if (2 * n < x) return 0;
if (x - 1 <= n) return x - 1;
return 2 * n - x + 1;
}
int main() {
scanf("%d%d", &n, &k);
rep(s, 2, 2 * n) {
int s2 = s - k;
if (s2 <= 1) continue;
res += 1LL * f(s) * f(s2);
}
printf("%lld\n", res);
}
| #include <bits/stdc++.h>
using namespace std;
class BinaryIndexedTree{
private:
const int N;
vector<int> a;
public:
BinaryIndexedTree(const int& N): N(N){
a.resize(N + 1, 0);
}
void add(const int& POS, const int& VAL){
for(int i = POS; i <= N; i += i - (i & (i - 1))){
a[i] += VAL;
}
}
int query(const int& POS){
int sum = 0;
for(int i = POS; i >= 1; i &= (i - 1)){
sum += a[i];
}
return sum;
}
int query(const int& L, const int& R){
if(L > R){
return 0;
}
return query(R) - query(L - 1);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<int> a(N + 1);
for(int i = 1; i <= N; ++i){
cin >> a[i];
a[i] += 1;
}
long long inversions = 0;
BinaryIndexedTree tree(N);
for(int i = 1; i <= N; ++i){
inversions += tree.query(a[i] + 1, N);
tree.add(a[i], 1);
}
cout << inversions << "\n";
for(int i = 1; i <= N - 1; ++i){
inversions -= (a[i] - 1);
inversions += (N - a[i]);
cout << inversions << "\n";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define MAX 100001
#define INF 1000000000000000000
#define MOD 1000000007
int main(){
vector<int> A(3);
for(int i=0;i<3;i++){
cin>>A[i];
}
sort(A.begin(),A.end());
if(A[2]-A[1]==A[1]-A[0]){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
| /*
JAI JAGANNATH!
*/
//@Author : zanj0
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x; cin>>x; while(x--)
#define endl "\n"
#define timetaken cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"
void zanj0()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve() {
vector <int> v(3);
for (int i = 0, x; i < 3; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
if (v[0] == v[1]) {
cout << v.back() << endl;
} else if (v[1] == v.back()) {
cout << v[0] << endl;
} else {
cout << 0 << endl;
}
}
int32_t main()
{
zanj0();
solve();
timetaken;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int n;
ll dp[N][2], w[N][2];
ll a[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
dp[1][0] = a[0];
w[1][0] = 1;
for (int i = 1; i < n; i++) {
dp[i + 1][0] = (dp[i][0] + dp[i][1]) % mod + (a[i] * w[i][0]) % mod +(a[i] * w[i][1]) % mod;
dp[i + 1][1] = (dp[i][0] - a[i] * w[i][0] %mod+ mod) % mod;
w[i + 1][0] = (w[i][0] + w[i][1]) % mod;
w[i + 1][1] = w[i][0];
}
printf("%lld\n", (dp[n][0] + dp[n][1]) % mod);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.14159265
ll mod1=1000000007;
// bool comp(pair<ll,ll>a,pair<ll,ll>b)
// {
// // if(a.second==b.second)
// // return a.first<b.first;
// // else
// return a.second<b.second;
// }
// sort(a.begin(),a.end(),greater<int>());
// priority_queue<int, vector<int>, greater<int> > Q;
// vector<ll> factorial(1001,1);
// vector<ll> inv_factorial(1001);
// vector<vector<ll> > combi(1001,vector<ll>(1001));
// ll binpow(ll a, ll b)
// {
// ll res = 1;
// while (b > 0) {
// if (b & 1)
// res = res * a;
// a = a * a;
// b >>= 1;
// }
// return res;
// }
// ll binpow(ll a, ll b, ll mod)
// {
// ll res = 1;
// while (b > 0) {
// if (b & 1)
// res = (res * a)%mod;
// a = (a * a)%mod;
// b >>= 1;
// }
// return res%mod;
// }
// 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 / gcd(a, b)) * b);
// }
// bool comp(ll a, ll b)
// {
// return a>b;
// }
// ll inversemod(ll a, ll mod)
// {
// return binpow(a,mod-2, mod);
// }
// for(int i=1;i<=1000;i++) factorial[i]=(factorial[i-1]*i)%mod1;
// for(int i=0;i<=1000;i++) inv_factorial[i]=inversemod(factorial[i],mod1);
// for(int i=0;i<=1000;i++)
// {
// for(int j=0;j<=i;j++)
// {
// combi[i][j]=(((factorial[i]*inv_factorial[i-j])%mod1)*inv_factorial[j])%mod1;
// }
// }
// ll t[4*20001]
// void build(ll a[], ll v, ll tl, ll tr) {
// if (tl == tr) {
// t[v] = a[tl];
// } else {
// int tm = (tl + tr) / 2;
// build(a, v*2, tl, tm);
// build(a, v*2+1, tm+1, tr);
// t[v] = t[v*2] ^ t[v*2+1];
// }
// }
// void dfs(ll i,ll *visited,vector<ll> e[])
// {
// visited[i]=1;
// ll n=e[i].size();
// for(ll j=0;j<n;j++)
// {
// if(visited[e[i][j]]==0)
// dfs(e[i][j],visited,e);
// }
// }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll test;
// cin>>test;
test=1;
for(ll ftest=0;ftest<test;ftest++)
{
ll n,i,j;
cin>>n;
ll A[n];
ll dp[n+1];
dp[0]=1;
dp[1]=1;
for(i=2;i<=n;i++)
dp[i]=(dp[i-1]+dp[i-2])%mod1;
ll ans=0;
for(i=0;i<n;i++)
{
cin>>A[i];
ans=(ans+((((A[i]*dp[i])%mod1)*dp[n-i])%mod1))%mod1;
if(i!=0)
ans=(ans-((((A[i]*dp[i-1])%mod1)*dp[n-i-1])%mod1))%mod1;
ans=(ans%mod1+mod1)%mod1;
// ans=(ans+x)%mod1;
}
cout<<ans<<'\n';
}
} |
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " is " << x << endl
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
int t, n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
while (t--) {
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i]; a[i]--;
}
vector<int> ans;
for (int i = 0; i < n; ++i) {
int j = find(a.begin(), a.end(), i) - a.begin();
if (i == j) continue;
if (j % 2 == (int)ans.size() % 2) {
if (j >= i + 2) {
ans.push_back(j - 2);
swap(a[j-2], a[j-1]);
}
else {
ans.push_back(i - 1);
ans.push_back(i);
ans.push_back(i - 1);
ans.push_back(i);
ans.push_back(i - 1);
swap(a[i], a[j]);
continue;
}
}
for (int k = j - 1; k >= i; --k) {
ans.push_back(k);
swap(a[k], a[k+1]);
}
}
cout << ans.size() << '\n';
for (int i = 0; i < (int)ans.size(); ++i) {
cout << ans[i] + 1 << ' ';
}
cout << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i,f,n) for(int i=f;i<n;i++)
#define FORI(i,f,n) for(int i=f;i>=n;i--)
#define sz(a) ((int)(a).size())
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define alli(a) (a).rbegin(),(a).rend()
#define approx(a) fixed<<setprecision(a)
#define trav(a,x) for(auto& a : x)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,bool> pib;
typedef pair<ll,bool> plb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <class T> void ckmax(T &a, const T &b) { a = max(a, b); }
template <class T> void read(vector<T> &v);
template <class F, class S> void read(pair<F, S> &p);
template <class T> void read(T &x) {cin>>x;}
template <class R, class... T> void read(R& r, T&... t){read(r); read(t...);};
template <class T> void read(vector<T> &v) {trav(x, v) read(x);}
template <class F, class S> void read(pair<F, S> &p) {read(p.ff, p.ss);}
template <class F, class S> void pr(const pair<F, S> &x);
template <class T> void pr(const T &x) {cout<<x;}
template <class R, class... T> void pr(const R& r, const T&... t) {pr(r); pr(t...);}
template <class F, class S> void pr(const pair<F, S> &x) {pr("{", x.ff, ", ", x.ss, "}");}
void ps() {pr("\n");}
template <class T> void ps(const T &x) {pr(x); ps();}
template <class T> void ps(vector<T> &v) {trav(x, v) pr(x, ' '); ps();}
template <class F, class S> void ps(const pair<F, S> &x) {pr(x.ff, ' ', x.ss); ps();}
template <class R, class... T> void ps(const R& r, const T &...t) {pr(r, ' '); ps(t...);}
const int MXN = 2e5+1;
vi adj[MXN], level[MXN], in(MXN), out(MXN);
int timer = 0;
void dfs(int v, int d){
in[v] = ++timer;
level[d].pb(in[v]);
for(int u : adj[v]){
dfs(u, d+1);
}
out[v] = ++timer;
}
void solve(){
int n; read(n);
FOR(i,1,n){
int x; read(x);
adj[x].pb(i+1);
}
dfs(1, 0);
int q; read(q);
FOR(i,0,q){
int u, d; read(u, d);
int ans = lower_bound(all(level[d]), out[u]) - lower_bound(all(level[d]), in[u]);
ps(ans);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define sz(v) (int)v.size();
#define ss second
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) x.begin(),x.end()
ll power(ll a, ll b){ll res=1;a=a%mod; while(b){if(b&1)res=(res*a)%mod; a=(a*a)%mod;b/=2;}return res;}
ll invmod(ll a){return power(a,mod-2);}
ll h,w,a,b;
ll dp[17][17];
int solve(int r,int c,int i,int j){
if(i<0||j<0) return 0;
if(c>=w){
c=0;
r++;
}
if(dp[r][c]) return solve(r,c+1,i,j);
if(r==h) return 1;
ll ans = solve(r,c+1,i,j-1);
dp[r][c]=1;
if(c+1<w && dp[r][c+1]==0) {
dp[r][c+1]=1;
ans += solve(r,c+2,i-1,j);
dp[r][c+1]=0;
}
if(r+1<h && dp[r+1][c]==0){
dp[r+1][c]=1;
ans+=solve(r,c+1,i-1,j);
dp[r+1][c]=0;
}
dp[r][c]=0;
return ans;
}
int main(void)
{
FIO
cin>>h>>w>>a>>b;
cout<<solve(0,0,a,b);
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main(void){
long long nCr[61][61]={};//0で初期化
nCr[0][0]=1;
for(int i=1;i<=60;i++){
for(int j=0;j<=i;j++){
if(j!=0){
nCr[i][j] += nCr[i-1][j-1];
}
if(j!=i){
nCr[i][j] += nCr[i-1][j];
}
}
}
long long A,B,K;
cin >> A >> B >> K;
string S;
long long C = A+B;
for(int i=0;i<C;i++){
//aがA個、bがB個ある
//前から(A+B-1)C(A-1)個は先頭がa
//その範囲に入った場合、a+((A+B-1)C(A-1)個中前からK個目)が答え
//そうでなければ、b+((A+B-1)C(A)個中前からK-(A+B-1)C(A-1)個目)が答え
if(A!=0 && K <= nCr[A+B-1][A-1]){
S.push_back('a');
A--;
}else{
S.push_back('b');
K -= nCr[A+B-1][A-1];
B--;
}
}
cout << S << endl;
return 0;
} |
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define p_q priority_queue
#define m_p make_pair
#define pb push_back
#define ld long double
#define F first
#define S second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll qpow(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string iots(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int stoi(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
int a,b,w;
int main()
{
ios::sync_with_stdio(0);
cin>>a>>b>>w;
w*=1000;
bool f=0;
forn(i,1,w)
{
if((ld)w/(ld)i<=(ld)b&&(ld)w/(ld)i>=(ld)a)
{
f=1;
}
}
if(!f)
{
cout<<"UNSATISFIABLE\n";
return 0;
}
cout<<(w+b-1)/b<<" "<<w/a<<endl;
return 0;
}
//特判判全
//循环边界
//精度足够
//注意初值
| #include<bits/stdc++.h>
using namespace std;
void solution()
{
double s, e, w;
cin >> s >> e >> w;
w *= 1000;
int end = floor(w / (double)s);
int start = ceil(w / (double)e);
int first = -1, last = -1;
bool flag = true;
for(int i = start; i <= end; i++)
{
if(w / w * w == w)
{
if(flag)
{
first = i;
flag = false;
}
last = i;
}
}
if(first < 0)
{cout << "UNSATISFIABLE" << endl;}
else
{cout << first << " " << last << endl;}
return;
}
int main()
{
int count, counter = 0;
count = 1;
while(counter++ < count)
{solution();}
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int flag = 1;
for(int i=0;i<n;i++)
{
if(a[i] != i+1)
{
cout<<"No"<<endl;
flag = 0;
break;
}
}
if(flag == 1)
{
cout<<"Yes"<<endl;
}
return 0;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}
const ll INF=1e18;
const int mx=200005;
//abc181
int main(){
cin.tie(0);ios::sync_with_stdio(false);
int n;
cin>>n;
if(n%2==0) out("White");
else out("Black");
}
//ペナルティ出しても焦らない ACできると信じろ!!!
//どうしてもわからないときはサンプルで実験 何か見えてくるかも
//頭で考えてダメなら紙におこせ!!
/*
V,P(大文字)使用不可
乗算などの際にオーバーフローに注意せよ!
(適切にmodをとれ にぶたんで途中で切り上げろ)
制約をよく読め!
{
・全探索できるなら全探索しろ
・異常な制約ならそこに注目
}
stringの計算量(扱い)注意
コーナー注意!(特に数値が小さいものについては要検証)
N行出力のときは'¥n'
グリッド上では行先が範囲内におさまるかif文で確認(RE注意)
if文ではちゃんと比較演算子==を使え('='でもエラー出ない)
配列(vector)の大きさが0か1以上かで場合分けせよ(RE注意)
(vector<int> a(m)でm==0というものはできない)
modはなるべく最後に取れ!
doubleを扱うときには(abs)ではなく'fabs'!!!
dpするときは「配る」、「もらう」の両方を考えて最適な方を選べ
Segtreeで区間最大(max)を使うとき,単位元の設定をしっかり
(デフォルトは-INF,0の方が都合の良い場合は変更)
*/ |
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
ll dp[1 << 20];
vector<int> pr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
int getmask(ll v) {
int mask = 0;
for (int i = 0; i < 20; i++) {
if (v % pr[i] == 0) mask |= (1 << i);
}
return mask;
}
void solve() {
ll a, b; cin >> a >> b;
dp[0] = 1;
for (ll v = a; v <= b; v++) {
int mymask = getmask(v);
for (int mask = 0; mask < (1 << 20); mask++) {
if (mask & mymask) continue;
dp[mask | mymask] += dp[mask];
}
}
cout << accumulate(dp, dp + (1 << 20), 0ll);
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
//cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
solve();
}
| typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
int main() {
ll n,k;
std::cin >> n>>k;
string s;
std::cin >> s;
string next = s+s;
for (int jjjj = 0; jjjj < k; jjjj++) {
s = next;
for (int i = 0; i < 2*n; i++) {
char x;
if(s[2*i%(2*n)]=='R'){
if(s[(2*i+1)%(2*n)]=='P'){
x= 'P';
}else{
x= 'R';
}
}else if(s[2*i%(2*n)]=='P'){
if(s[(2*i+1)%(2*n)]=='S'){
x= 'S';
}else{
x= 'P';
}
}else{
if(s[(2*i+1)%(2*n)]=='R'){
x= 'R';
}else{
x= 'S';
}
}
next[i] =x;
}
}
std::cout << next[0] << std::endl;
}
|
//Codeforcesで128bit整数を使いたいとき
//→__int128_tを使う&GNU C++17 (64)で提出する
//インクルードなど
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//イテレーション
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl;
//aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}
// bool isOK(string X,ll base,ll M) {
// ll sum = 0;
// REP(i, X.size()) {
// if (sum > M / base) {
// return false;
// }
// sum *= base;
// sum += (X.at(i) - '0');
// }
// if (sum <= M) {
// return true;
// } else {
// return false;
// }
// }
bool isOK(string X, ll key,ll M) {
ll result=0,d = 1;
for(ll i=X.size()-1;i >= 0;i--,d*=key){
if (M/d < (X[i]-'0')) return false;
if (result > M - (X[i]-'0')*d) return false;
result += (X[i]-'0')*d;
if(i >= 1){
if(M/key < d){
return false;
}
}
}
//cout << "result = " << result << "\n";
if (result <= M) return true;
else return false;
}
int main(){
string X;
ll M;
ll max;
cin >> X >> M;
max = X[0] - '0';
for (ll i = 1; i < (ll)X.size(); i++) {
if (max < X[i] - '0') {
max = X[i] - '0';
}
}
ll left = M+10, right = max;
ll mid=0;
if(X.size()==1){
if (X[0]-'0' <= M) cout << 1 << "\n";
else cout << 0 << "\n";
return 0;
}
while(left - right > 1){
mid = (left + right) / 2;
//cout << "L R = " << left << " " << right << "\n";
//cout << "mid = " << mid << "\n";
if(isOK(X,mid,M)) right = mid;
else left = mid;
}
cout << right - max << "\n";
return 0;
} | #pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
void iosetup(void){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
int get_d(string& X){
int ret = 0;
for (auto d : X)
ret = max(ret, (int)(d - '0'));
return ret;
}
constexpr ll INF = (1LL << 61);
ll n_num(string& X, ll n){
ll ret = 0LL;
for (auto d : X){
if (ret > INF / n) return INF;
ret = ret * n + (ll)(d - '0');
}
return ret;
}
ll solve(string& X, ll M){
int d = get_d(X);
if (n_num(X, d + 1) > M) return 0;
ll ok = d + 1, ng = INF;
while (ng - ok > 1){
ll mid = (ok + ng) / 2;
if (n_num(X, mid) > M) ng = mid;
else ok = mid;
}
return (ng - d - 1);
}
int main(void){
iosetup();
string X;
ll M;
cin >> X >> M;
if (X == "1") cout << 1 << endl;
else if (X.length() == 1){
ll Xi = (ll)(X[0] - '0');
if (Xi <= M) cout << 1 << endl;
else cout << 0 << endl;
}
else cout << solve(X, M) << endl;
return 0;
} |
Subsets and Splits