solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int solve(int n) {
int cnt = 0;
for (int i = 1;i <= n / 2; i++) {
int m = 0;
int j = i;
while(m < n) m += j++;
if (m == n) cnt++;
}
return cnt;
}
int main() {
int n;
while(cin >> n, n) {
cout << solve(n) << endl;
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll e=0,f=0;
vector<int> G[200001];
map<P,ll> A;
void dfs(int x,int y,int z=-1){
if(x==y){
f=1;
return;
}
for(auto i:G[x]){
if(i==z) continue;
if(f==1) continue;
dfs(i,y,x);
if(f==1) A[P(min(i,x),max(i,x))]+=1<<e;
}
}
int main(){
ll n,m,a[50],b[50],u,v,B[50],ans=0;
cin>>n;
rep(i,n-1){
cin>>a[i]>>b[i];
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
cin>>m;
rep(i,m) cin>>u>>v,dfs(u,v),e++,f=0;
rep(i,n-1) B[i]=A[P(min(a[i],b[i]),max(a[i],b[i]))];
for(int i=1;i<(1<<m);i++){
ll num=0,x=0;
for(int j=i;j!=0;j>>=1) num+=j&1;
rep(k,n-1){
int z=1;
rep(j,m){
if(i>>j&1){
if(B[k]>>j&1) z=0;
}
}
if(z) x++;
}
if(num%2) ans+=ll(1)<<x;
else ans-=ll(1)<<x;
}
cout<<(ll(1)<<n-1)-ans<<"\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
string str;
cin >> str;
int c1 = 0, c2 = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '0') {
if (c1)
printf("3 1\n"), c1--;
else
printf("1 1\n"), c1++;
} else {
if (c2)
printf("4 1\n"), c2--;
else
printf("4 3\n"), c2++;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int32_t main() {
string s;
cin >> s;
long long n = s.size();
map<long long, vector<long long> > h;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == 'A' && s[i + 1] == 'B') {
h[0].push_back(i);
}
if (s[i] == 'B' && s[i + 1] == 'A') {
h[1].push_back(i);
}
}
long long f = 0;
for (auto sp1 : h[0]) {
for (auto sp2 : h[1]) {
if (abs(sp1 - sp2) >= 2) {
f = 1;
cout << "YES"
<< "\n";
break;
}
}
if (f == 1) {
break;
}
}
if (f == 0) {
cout << "NO"
<< "\n";
}
return 0;
}
| 1 |
#include <iostream>
using namespace std;
char conv[128];
void make() {
for(int i = 0; i < 128; ++i) conv[i] = i;
}
int main() {
int n, m;
while((cin >> n) && n) {
make();
for(int i = 0; i < n; ++i) {
char a, b;
cin >> a >> b;
conv[a] = b;
}
cin >> m;
for(int i = 0; i < m; ++i) {
char c;
cin >> c;
cout << conv[c];
}
cout << endl;
}
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
T gcd(T a, T b) {
return ((b == 0) ? a : gcd(b, a % b));
}
bool cmp(pair<string, int> a, pair<string, int> b) {
return a.first.size() < b.first.size();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pair<string, int>> s(2 * n - 2);
for (int i = 0; i < (int)(2 * n - 2); i++) cin >> s[i].first, s[i].second = i;
sort(s.begin(), s.end(), cmp);
for (int i = 0; i < (int)(2); i++) {
string pref = s[2 * n - 3].first;
string suf = s[2 * n - 4].first;
if (i == 1) swap(pref, suf);
reverse(suf.begin(), suf.end());
vector<char> ans(2 * n - 2);
map<string, int> vis;
int f = 1;
for (int j = 0; j < (int)(2 * n - 2); j++) {
string ltl = pref.substr(0, s[j].first.size());
string sm = suf.substr(0, s[j].first.size());
string cur = s[j].first;
reverse(cur.begin(), cur.end());
if (ltl == s[j].first && !vis[ltl]) {
ans[s[j].second] = 'P';
vis[ltl] = 1;
} else if (sm == cur) {
ans[s[j].second] = 'S';
} else
f = 0;
}
if (f) {
for (int k = 0; k < (int)(ans.size()); k++) cout << ans[k];
cout << endl;
return 0;
}
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
using Double = long double;
signed main(){
Int h,w;
cin>>w>>h;
vector<string> s(h);
for(Int i=0;i<h;i++) cin>>s[i];
vector<vector<Int> > dg(h,vector<Int>(w,-1)),ds=dg;
using T = pair<Int,int>;
queue<T> qg,qs;
for(Int i=0;i<h;i++){
for(Int j=0;j<w;j++){
if(s[i][j]=='g'){
qg.push(T(i,j));
dg[i][j]=0;
}
if(s[i][j]=='*'){
qs.push(T(i,j));
ds[i][j]=0;
}
}
}
Int dy[]={0,0,1,-1};
Int dx[]={1,-1,0,0};
auto bfs=[&](queue<T> &q,vector<vector<Int> > &d){
while(!q.empty()){
T t=q.front();q.pop();
Int y=t.first,x=t.second;
for(Int k=0;k<4;k++){
Int ny=y+dy[k],nx=x+dx[k];
if(s[ny][nx]=='#'||s[ny][nx]=='*') continue;
if(~d[ny][nx]&&d[ny][nx]<=d[y][x]+1) continue;
d[ny][nx]=d[y][x]+1;
q.push(T(ny,nx));
}
}
if(0){
cout<<endl;
for(Int i=0;i<h;i++){
for(Int j=0;j<w;j++){
if(d[i][j]<0) cout<<"x";
else cout<<hex<<d[i][j];
}
cout<<endl;
}
}
};
bfs(qg,dg);
bfs(qs,ds);
auto get=[&](Int i,Int j,Double p){
if(~dg[i][j]&&~ds[i][j])
return min((Double)dg[i][j],ds[i][j]+p);
if(~ds[i][j]) return ds[i][j]+p;
if(~dg[i][j]) return (Double)dg[i][j];
return Double(0);
};
auto calc=[&](Double p){
Double q=0,c=0;
for(Int i=0;i<h;i++){
for(Int j=0;j<w;j++){
if(s[i][j]=='#'||s[i][j]=='g'||s[i][j]=='*') continue;
c+=1.0;
q+=get(i,j,p);
}
}
q/=c;
//printf("%.12Lf %.12Lf\n",p, p-q);
return p-q;
};
Double l=0,r=1e15;
for(int k=0;k<300;k++){
//while(abs(calc(l))>1e-10){
Double m=(l+r)/2;
if(calc(m)<=Double(0)) l=m;
else r=m;
}
//printf("%.12f\n",calc(l));
for(Int i=0;i<h;i++)
for(Int j=0;j<w;j++)
if(s[i][j]=='s')
printf("%.12Lf\n",get(i,j,l));
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, x;
cin >> n >> x;
int odd = 0, even = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
(a & 1 ? odd : even) += 1;
}
bool ok = false;
for (int i = 1; i <= x; i += 2) ok |= (odd >= i && even >= x - i);
cout << (ok ? "Yes\n" : "No\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) solve();
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[50],b[50];
int A[51],B[51];
int dp[51][51][51][51][2][2];
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==-1)a[i]=0;
A[i+1]=A[i]+a[i];
}
for(int i=0;i<m;i++){
cin>>b[i];
if(b[i]==-1)b[i]=0;
B[i+1]=B[i]+b[i];
}
memset( dp, -1, sizeof(dp));
for(int x=n;x>=0;x--)
for(int y=m;y>=0;y--)
for(int s=x;s>=0;s--)
for(int t=y;t>=0;t--)
for(int f=1;f>=0;f--)
for(int p=1;p>=0;p--){
int &res=dp[x][y][s][t][f][p];
if(p==0){
res=-1e9;
if(f==1&&x==s&&y==t)res=max(res,0);
else res=max(res,dp[x][y][x][y][x==s&&y==t][1]+A[x]-A[s]-B[y]+B[t]);
if(x<n&&a[x]==0)res=max(res,dp[x+1][y][s][y][0][1]);
if(x<n&&a[x]>0)res=max(res,dp[x+1][y][s][t][0][1]);
}else{
res=1e9;
if(f==1&&x==s&&y==t)res=min(res,0);
else res=min(res,dp[x][y][x][y][x==s&&y==t][0]+A[x]-A[s]-B[y]+B[t]);
if(y<m && b[y]==0)res=min(res,dp[x][y+1][x][t][0][0]);
if(y<m && b[y]>0)res=min(res,dp[x][y+1][s][t][0][0]);
}
}
cout<<dp[0][0][0][0][0][0]<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1100];
double f[1100][1100], ans = 0;
int main() {
scanf("%d%d", &n, &m);
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
f[i][j] = (a[i] > a[j]), f[j][i] = 1 - f[i][j];
for (int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
f[x][y] = f[y][x] = 0.5;
for (int j = 1; j <= n; ++j)
if (j != x && j != y) {
f[x][j] = f[y][j] = (f[x][j] + f[y][j]) * 0.5;
f[j][x] = f[j][y] = (f[j][x] + f[j][y]) * 0.5;
}
}
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j) ans += f[i][j];
printf("%.10f\n", ans);
return 0;
}
| 4 |
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 210
#define inf 2147483647
using namespace std;
struct edge{int x, y, l, next;}a[100010];
int n, m, l, p[N], T, x1, y1, x2, y2, lev[N], q[N], ans;
char S[N];
inline void add(int x, int y, int z){
a[++l].x=x; a[l].y=y; a[l].l=z; a[l].next=p[x]; p[x]=l;
a[++l].x=y; a[l].y=x; a[l].l=0; a[l].next=p[y]; p[y]=l;
}
inline int bfs(){
queue<int>q; q.push(0);
memset(lev, 0, sizeof(lev)); lev[0]=1;
while(!q.empty()){
int v=q.front(); q.pop();
for(int i=p[v]; i; i=a[i].next)
if(a[i].l&&!lev[a[i].y]){
lev[a[i].y]=lev[v]+1;
if(a[i].y==T)return 1;
q.push(a[i].y);
}
}
return 0;
}
inline int dfs(int x, int ss){
if(x==T||!ss)return ss;
int s1=ss;
for(int i=q[x]; i&&ss; i=a[i].next)
if(lev[a[i].y]==lev[x]+1){
int tt=dfs(a[i].y, min(ss, a[i].l));
q[x]=i; ss-=tt; a[i].l-=tt; a[i^1].l+=tt;
}
if(s1==ss)lev[x]=-1;
return s1-ss;
}
int main(){
scanf("%d%d", &n, &m);
l=1; memset(p, 0, sizeof(p)); T=n+m+1;
for(int i=1; i<=n; i++){
scanf("%s", S+1);
for(int j=1; j<=m; j++){
if(S[j]=='o'){add(i, n+j, 1); add(n+j, i, 1);}
if(S[j]=='S'){x1=i; y1=j;}
if(S[j]=='T'){x2=i; y2=j;}
}
}
if(x1==x2||y1==y2){printf("-1"); return 0;}
add(0, x1, inf); add(0, n+y1, inf);
add(x2, T, inf); add(n+y2, T, inf);
ans=0; while(bfs()){memcpy(q, p, sizeof(q)); ans+=dfs(0, inf);}
printf("%d", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int temp;
vector<int> arr;
bool isprime(int x) {
if (x == 2 || x == 3) return true;
for (int i = 2; i <= sqrt(x); i++)
if (x % i == 0) return false;
return true;
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
if (isprime(i) == true) {
temp = i;
while (temp <= n) {
arr.push_back(temp);
temp *= i;
}
}
}
printf("%d\n", arr.size());
for (int i = 0; i < arr.size(); i++) printf("%d ", arr[i]);
return 0;
}
| 1 |
//AOJ0118
#include<iostream>
#include<string>
using namespace std;
int H,W;
char table[102][102];
void kesu(int y,int x,char now){
if(table[y][x]!=now)
return ;
table[y][x]='x';
kesu(y+1,x,now);
kesu(y-1,x,now);
kesu(y,x+1,now);
kesu(y,x-1,now);
}
int main(){
while(true){
cin>>H>>W;
if(H==0&&W==0)break;
for(int i=0;i<102;i++)for(int j=0;j<102;j++)table[i][j]='x';
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++){
char tmp;
cin>>tmp;
table[i][j]=tmp;
}
}
int ans;
ans=0;
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++){
if(table[i][j]!='x'){
kesu(i,j,table[i][j]);
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; ++i)
#define all(a) (a).begin(), (a).end()
ll N;
ll M;
vector<ll> A;
int main() {
cin >> N >> M;
A.resize(N);
rep(i, N) cin >> A[i];
sort(all(A));
vector<ll> S(N + 1, 0);
rep(i, N) S[i + 1] = S[i] + A[i];
function<ll(ll)> num = [&](ll t) {
ll res = 0;
rep(i, N) res += distance(lower_bound(all(A), t - A[i]), A.end());
return res;
};
ll l = 0, r = 1e6;
while (r - l > 1) {
ll m = (l + r) / 2;
if (num(m) <= M) {
r = m;
} else {
l = m;
}
}
ll ans = 0;
ll rest = M;
rep(i, N) {
int l = distance(lower_bound(all(A), r - A[i]), A.end());
ans += l * A[i] + S[N] - S[N - l];
rest -= l;
}
ans += rest * l;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
void print_array(int *a, int n) {
printf("#");
for (int i = 0; i < n; i++) printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
}
using namespace std;
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
int mp[200][200];
int n, m, h;
void optr(int i, int j) {
int rowmax = 0;
for (int k = 0; k < m; ++k) {
if (k == j) continue;
rowmax = max(rowmax, mp[i][k]);
}
int colmax = 0;
for (int k = 0; k < n; ++k) {
if (k == i) continue;
colmax = max(colmax, mp[k][j]);
}
if (rowmax > colmax) swap(rowmax, colmax);
if (mp[i][j] <= rowmax) {
mp[i][j] = 1;
} else if (mp[i][j] <= colmax) {
} else {
}
}
int main() {
scanf("%d%d%d", &n, &m, &h);
int x;
for (int i = 0; i < m; ++i) {
scanf("%d", &x);
for (int j = 0; j < n; ++j) {
mp[j][i] = x;
}
}
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
for (int j = 0; j < m; ++j) {
mp[i][j] = min(mp[i][j], x);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf("%d", &x);
if (x == 0) mp[i][j] = 0;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mp[i][j] == 0) continue;
optr(i, j);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("%d", mp[i][j]);
if (j != m - 1) putchar(' ');
}
puts("");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int n, A, B, m;
int a[200010], opa[200010], opb[200010];
vector<int> G[200010];
vector<int> rG[200010];
vector<int> vs;
bool used[200010];
int cmp[200010];
void addedge(int u, int v) {
G[u].push_back(v);
rG[v].push_back(u);
}
void init() {
for (int i = 0; i <= n; ++i) {
G[i].clear();
rG[i].clear();
}
}
void dfs(int u) {
used[u] = true;
for (int i = 0; i < G[u].size(); ++i) {
if (!used[G[u][i]]) dfs(G[u][i]);
}
vs.push_back(u);
}
void rdfs(int u, int k) {
used[u] = true;
cmp[u] = k;
for (int i = 0; i < rG[u].size(); ++i) {
if (!used[rG[u][i]]) rdfs(rG[u][i], k);
}
}
int scc() {
memset(used, 0, sizeof(used));
vs.clear();
for (int i = 0; i < n; ++i) {
if (!used[i]) dfs(i);
}
memset(used, 0, sizeof(used));
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i) {
if (!used[vs[i]]) rdfs(vs[i], k++);
}
return k;
}
int col[200010];
void build() {
for (int i = 0; i < m; ++i) {
int x = opa[i];
int y = opb[i];
if (opa[i] >= 0 && opb[i] >= 0) {
addedge(i, x);
addedge(i, y);
addedge(i + m, x + m);
addedge(i + m, y + m);
} else if (opa[i] < 0) {
addedge(i + m, y + m);
addedge(i, i + m);
} else if (opb[i] < 0) {
addedge(i, x);
addedge(i + m, i);
}
}
}
void solve() {
build();
int k = scc();
for (int i = 0; i < m; ++i) {
if (cmp[i] == cmp[m + i]) {
printf("NO\n");
return;
}
}
printf("YES\n");
for (int i = 0; i < m; ++i) {
if (cmp[i] > cmp[i + m]) {
col[i] = 0;
} else {
col[i] = 1;
}
printf("%d ", col[i]);
}
printf("\n");
}
int main() {
while (scanf("%d%d%d", &m, &A, &B) != EOF) {
mp.clear();
for (int i = 0; i < m; ++i) {
scanf("%d", &a[i]);
mp[a[i]] = i + 1;
opa[i] = opb[i] = -1;
}
for (int i = 0; i < m; ++i) {
if (A > a[i]) {
if (mp[A - a[i]] > 0) {
opa[i] = mp[A - a[i]] - 1;
}
}
if (B > a[i]) {
if (mp[B - a[i]] > 0) {
opb[i] = mp[B - a[i]] - 1;
}
}
}
init();
n = 2 * m;
bool flag = true;
for (int i = 0; i < n; ++i) {
if (opa[i] < 0 && opb[i] < 0) {
flag = false;
break;
}
}
if (!flag) {
printf("NO\n");
continue;
}
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000007;
const long long int inf = 1e15;
void __print(long long int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
long long int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void tc() {
string s;
cin >> s;
long long int n = s.length();
for (long long int i = 0; i < n; i++) {
if (s[i] == '0') {
cout << "YES" << endl;
cout << 0 << endl;
return;
}
if (s[i] == '8') {
cout << "YES" << endl;
cout << 8 << endl;
return;
}
}
for (long long int h = 0; h <= n - 3; h++) {
for (long long int t = h + 1; t <= n - 2; t++) {
for (long long int u = t + 1; u <= n - 1; u++) {
long long int num =
(s[h] - '0') * 100 + (s[t] - '0') * 10 + (s[u] - '0');
if (num % 8 == 0) {
cout << "YES" << endl;
cout << num << endl;
return;
}
}
}
}
for (long long int t = 0; t <= n - 2; t++) {
for (long long int u = t + 1; u <= n - 1; u++) {
long long int num = (s[t] - '0') * 10 + (s[u] - '0');
if (num % 8 == 0) {
cout << "YES" << endl;
cout << num << endl;
return;
}
}
}
cout << "NO" << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
while (t--) {
tc();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return a * (b / gcd(a, b));
}
void solve() {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int ans = 0;
if (n == 1) {
cout << a[0];
return;
}
if (n == 2) {
cout << (a[0] | a[1]);
return;
}
for (long long int i = 0; i < n; i++) {
for (long long int j = i + 1; j < n; j++) {
for (long long int k = j + 1; k < n; k++) {
ans = max(ans, a[i] | a[j] | a[k]);
}
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int TESTS = 1;
while (TESTS--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n, k, c[500100];
int main() {
while (scanf("%d%d", &n, &k) > 0) {
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]);
}
long long lo = 0, up = (long long)5e15, val;
while (lo <= up) {
long long mid = (lo + up) >> 1, cnt = 0;
for (int i = 0; i < n and cnt <= k; i++) {
if (c[i] < mid) {
cnt += mid - c[i];
}
}
if (cnt <= k) {
val = mid;
lo = mid + 1;
} else {
up = mid - 1;
}
}
int aux = k;
for (int i = 0; i < n; i++) {
if (c[i] < val) {
aux -= val - c[i];
c[i] = val;
}
}
for (int i = 0; i < n and aux; i++) {
if (c[i] == val) {
c[i]++;
aux--;
}
}
lo = 1;
up = (int)5e15;
while (lo <= up) {
long long mid = (lo + up) >> 1, cnt = 0;
for (int i = 0; i < n and cnt <= k; i++) {
if (c[i] > mid) {
cnt += c[i] - mid;
}
}
if (cnt <= k) {
val = mid;
up = mid - 1;
} else {
lo = mid + 1;
}
}
aux = k;
for (int i = 0; i < n; i++) {
if (c[i] > val) {
aux -= c[i] - val;
c[i] = val;
}
}
for (int i = 0; i < n and aux; i++) {
if (c[i] == val) {
c[i]--;
aux--;
}
}
int p = 0, r = 0;
for (int i = 0; i < n; i++) {
if (c[i] > c[r]) r = i;
if (c[i] < c[p]) p = i;
}
printf("%d\n", c[r] - c[p]);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,k,a,b=0;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a;
if(a>k-a)
b+=2*(k-a);
else
b+=2*a;
}
cout<<b<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool check(char s[]) {
int i = 0;
int j = strlen(s) - 1;
while (i <= j) {
if (s[i] != s[j]) return false;
i++;
j--;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
char S[100], s[100];
cin >> S;
int n = strlen(S);
for (int i = 'a'; i <= 'z'; ++i) {
for (int j = 0; j <= n; ++j) {
int x = 0;
for (int k = 0; k < j; ++k) {
s[x++] = S[k];
}
s[x++] = i;
for (int k = j; k < n; ++k) {
s[x++] = S[k];
}
s[x] = '\0';
if (check(s)) {
cout << s << endl;
return 0;
}
}
}
cout << "NA" << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3000;
int mat[N][N];
vector<int> adj[N];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u;
--v;
mat[u][v] = 1;
adj[u].push_back(v);
}
long long ans = 0;
for (int u = 0; u < n; ++u) {
for (int v = 0; v < n; ++v) {
if (u == v) continue;
long long cnt = 0;
for (int w : adj[u]) {
if (mat[w][v]) cnt++;
}
ans += cnt * (cnt - 1) / 2;
}
}
printf("%I64d\n", ans);
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
printf("%d %d %d",c,a,b);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
inline T bigmod(T p, T e, T M) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigmod(p, e / 2, M);
return (T)((t * t) % M);
}
return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M;
}
template <class T>
inline T bigexp(T p, T e) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigexp(p, e / 2);
return (T)((t * t));
}
return (T)((long long int)bigexp(p, e - 1) * (long long int)p);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
int dx4[] = {1, 0, -1, 0};
int dy4[] = {0, 1, 0, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n, a[100005];
int main() {
scanf("%d", &n);
for (__typeof(n - 1) i = (1); i <= (n - 1); i++) a[i] = i + 1;
a[n] = 1;
for (__typeof(n) i = (1); i <= (n); i++) {
if (i > 1) printf(" ");
printf("%d", a[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3030, M = 1e4 + 100;
int n, S, ans, u[N], v[N], w[N], par[N], sz[N], cnt[N];
bool bad;
vector<int> vec[M];
int g_par(int v) { return v == par[v] ? v : par[v] = g_par(par[v]); }
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> u[i] >> v[i] >> w[i];
u[i]--;
v[i]--;
vec[w[i]].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> sz[i];
par[i] = i;
cnt[i] = 1;
S += sz[i];
}
for (int w = 0; w < M && bad == false; w++) {
ans = w;
for (int dex : vec[w]) {
int U = g_par(u[dex]), V = g_par(v[dex]);
sz[U] += sz[V];
cnt[U] += cnt[V];
par[V] = U;
if (S - sz[U] < cnt[U]) bad = true;
}
}
cout << (n == 1 ? 0 : ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int m, n, min = 0, max = 0;
scanf("%d", &n);
m = n;
if (n < 7) {
if (n < 3) {
min = 0;
max = n;
} else if (n < 6) {
min = 0;
max = 2;
} else {
min = 1;
max = 2;
}
} else {
if (n % 7 == 0) {
max = (n / 7) * 2;
min = (n / 7) * 2;
} else {
min = (n / 7) * 2;
n -= (n / 7) * 7;
if (n == 6) min++;
max = (m / 7) * 2;
m -= (m / 7) * 7;
if (m < 3)
max += m;
else
max += 2;
}
}
printf("%d %d", min, max);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using cd = complex<double>;
using moveset = array<list<int>, 3>;
struct formula {
int period_length;
vector<int> values;
};
formula deduce_formula(moveset m) {
static constexpr int bulan = 200;
vector<int> vals(2 * bulan, 0);
for (int i = 3; i < 2 * bulan; ++i) {
bool freq[10] = {};
for (auto j : m[i % 3]) freq[vals[max(i - j, 0)]] = true;
while (freq[vals[i]]) ++vals[i];
}
int dist = 1;
auto check_dist = [&]() {
for (int i = bulan; i < 2 * bulan - dist; ++i)
if (vals[i] != vals[i + dist]) return false;
return true;
};
while (!check_dist()) ++dist;
return formula{dist, vector<int>{begin(vals), begin(vals) + bulan + dist}};
}
int apply_formula(formula f, ll x) {
if (x < (ll)f.values.size())
return f.values[x];
else
return f.values[(x - f.values.size() + f.period_length) % f.period_length +
f.values.size() - f.period_length];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, x, y, z;
cin >> n >> x >> y >> z;
moveset m;
m[0].push_back(3 * x);
m[0].push_back(3 * y - 1);
m[0].push_back(3 * z - 2);
m[1].push_back(3 * x + 1);
m[1].push_back(3 * z - 1);
m[2].push_back(3 * x + 2);
m[2].push_back(3 * y + 1);
formula f = deduce_formula(m);
vector<ll> v(n);
for (auto& x : v) {
cin >> x;
x *= 3;
}
ll my_xor = 0;
for (auto x : v) my_xor ^= apply_formula(f, x);
ll ret = 0;
for (auto x : v)
for (auto y : m[x % 3]) {
ll tmp =
my_xor ^ apply_formula(f, x) ^ apply_formula(f, max<ll>(x - y, 0));
if (tmp == 0) ++ret;
}
cout << ret << '\n';
}
return 0;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MOD=1e9+7;
const int INF=1e9;
const ll LINF=(ll)1e18;
ll modpow(ll x,ll n){
ll res=1;
while(n>0){
if(n&1)res=res*x%MOD;
x=x*x%MOD;
n>>=1;
}
return res;
}
struct Combination{
vector<ll> fac,finv,inv;
Combination(int mx){init(mx);}
void init(int mx){
fac.resize(mx+1);
finv.resize(mx+1);
inv.resize(mx+1);
fac[0]=fac[1]=1;
finv[0]=finv[1]=1;
inv[1]=1;
for(int i=2;i<=mx;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;
}
}
ll operator()(int n,int k){
if(n<k)return 0;
return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
};
int main(){
int b,w;
cin>>b>>w;
ll half=modpow(2,MOD-2);
ll s=0,t=0;
Combination comb(b+w);
for(int i=1;i<=b+w;i++){
ll ans=(half*(1+s-t+MOD))%MOD;
cout<<ans<<endl;
if(i>=w){
s=(s+comb(i-1,w-1)*modpow(half,i))%MOD;
}
if(i>=b){
t=(t+comb(i-1,b-1)*modpow(half,i))%MOD;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int inf = numeric_limits<int>::max();
int main() {
int al, ar, bl, br;
cin >> al >> ar >> bl >> br;
cout << ((br <= (al + 1) * 2 && br >= al - 1) ||
(bl <= (ar + 1) * 2 && bl >= ar - 1)
? "YES"
: "NO")
<< endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int pal[25];
vector<int> g[N];
vector<int> comp[N];
int cnt[(1 << 21) + 1337];
bool used[N];
int let[N];
long long ans[N];
int f[30][N];
void init() {
for (int i = 0; i < (N - 1); ++i) used[i] = 0;
for (int i = 0; i < (N - 1); ++i) ans[i] = 0ll;
for (int i = 0; i < (20); ++i) pal[i] = (1 << i);
pal[20] = 0;
for (int i = 0; i < ((1 << 21) + 333); ++i) cnt[i] = 0;
}
int find_center(int v, int scale, int ¢er, int p) {
int sz = 1;
for (auto u : g[v]) {
if (u != p && !used[u]) {
sz += find_center(u, scale, center, v);
}
}
if (center == -1 && (scale <= sz + sz || p == -1)) {
center = v;
}
return sz;
}
void dfs_cnt(int v, int p, int mask, int add) {
mask ^= let[v];
cnt[mask] += add;
for (auto u : g[v]) {
if (!used[u] && u != p) {
dfs_cnt(u, v, mask, add);
}
}
}
long long dfs_calc(int v, int p, int mask, long long &d) {
mask ^= let[v];
long long new_path = 0;
for (int pi = 0; pi < 21; ++pi) {
int plnd = pal[pi];
int need = plnd ^ mask;
if (need == 0) ++d;
new_path += cnt[need];
}
for (auto u : g[v]) {
if (!used[u] && u != p) {
new_path += dfs_calc(u, v, mask, d);
}
}
ans[v] += new_path;
return new_path;
}
void build(int v, int scale) {
int c = -1;
find_center(v, scale, c, -1);
used[c] = true;
cnt[0]++;
for (auto u : g[c]) {
if (!used[u]) {
dfs_cnt(u, c, 0, 1);
}
}
long long d = 0;
for (auto u : g[c]) {
if (!used[u]) {
dfs_cnt(u, c, 0, -1);
d += dfs_calc(u, c, let[c], d);
dfs_cnt(u, c, 0, 1);
}
}
ans[c] += d / 2;
cnt[0]--;
for (auto u : g[c]) {
if (!used[u]) {
dfs_cnt(u, c, 0, -1);
}
}
for (auto u : g[c]) {
if (!used[u]) {
build(u, scale / 2 + scale % 2);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
init();
int n;
cin >> n;
for (int i = 0; i < (n - 1); ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
string s;
cin >> s;
for (int i = 0; i < (n); ++i) {
let[i] = (1 << (s[i] - 'a'));
}
build(0, n);
for (int i = 0; i < n; ++i) cout << ans[i] + 1 << ' ';
cout << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], k[N];
void init(int n) {
for (int i = 1; i <= n; i++) {
k[i] = 0;
}
}
int main() {
ios_base::sync_with_stdio(0);
int n, d, b;
cin >> n >> d >> b;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (n == 2) {
cout << "0\n";
return 0;
}
int l = 0, r = n * b;
while (l != r) {
int mid = (l + r + 1) / 2;
init(n);
int z = 1;
int sum = 0;
while (sum < mid) {
if (a[z] < mid - sum) {
sum += a[z];
k[z] = a[z];
} else {
k[z] = mid - sum;
sum = mid;
}
z++;
}
int buf = k[1];
int j = 2;
int ans1 = 0;
for (int i = 1; i <= n / 2 + (n & 1); i++) {
while (j <= n && j <= i + (long long)i * d) {
buf += k[j];
j++;
}
if (buf >= b) {
buf -= b;
} else {
ans1++;
}
}
init(n);
sum = 0;
z = n;
while (sum < n * b - mid) {
if (a[z] < n * b - mid - sum) {
k[z] = a[z];
sum += a[z];
} else {
k[z] = n * b - mid - sum;
sum = n * b - mid;
}
z--;
}
buf = k[n];
j = n - 1;
int ans2 = 0;
for (int i = 1; i <= n / 2; i++) {
while (j > 0 && j >= n - i + 1 - (long long)i * d) {
buf += k[j];
j--;
}
if (buf >= b) {
buf -= b;
} else {
ans2++;
}
}
if (ans1 >= ans2) {
l = mid;
} else {
r = mid - 1;
}
}
int mid = l;
init(n);
int z = 1;
int sum = 0;
while (sum < mid) {
if (a[z] < mid - sum) {
sum += a[z];
k[z] = a[z];
} else {
k[z] = mid - sum;
sum = mid;
}
z++;
}
int buf = k[1];
int j = 2;
int ans1 = 0;
for (int i = 1; i <= n / 2 + (n & 1); i++) {
while (j <= n && j <= i + (long long)i * d) {
buf += k[j];
j++;
}
if (buf >= b) {
buf -= b;
} else {
ans1++;
}
}
cout << ans1 << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
double ans = 0;
int ne, nf, ns;
double le, lf, ls;
int rf, re, rs;
int de, df;
double interFLen[30][30];
double interELen[30][30];
double inter(double st1, double ed1, double st2, double ed2) {
if (st2 > ed1 || st1 > ed2) return 0;
return min(ed1, ed2) - max(st1, st2);
}
int place[30];
void calc() {
static double winF[30];
static double winE[30];
for (int i = 1; i <= 13; i++) {
winF[i] = 2 * lf;
winE[i] = 2 * le;
for (int j = 1; j <= 13; j++) {
winF[i] += interFLen[i][j] * place[j];
winE[i] += interELen[i][j] * place[j];
}
winF[i] *= df;
winE[i] *= de;
}
static double f[30][30];
memset(f, 0, sizeof(f));
for (int pos = 1; pos <= 13; pos++) {
for (int times = 1; times <= 2 - place[pos]; times++) {
for (int i = nf; i >= 0; i--) {
for (int j = ne; j >= 0; j--) {
if (i) f[i][j] = max(f[i][j], f[i - 1][j] + winF[pos]);
if (j) f[i][j] = max(f[i][j], f[i][j - 1] + winE[pos]);
}
}
}
}
ans = max(ans, f[nf][ne]);
}
void dfs(int id, int left) {
if (id == 14) {
if (!left) calc();
return;
}
dfs(id + 1, left);
if (left >= 1) {
place[id] = 1;
dfs(id + 1, left - 1);
place[id] = 0;
}
if (left >= 2) {
place[id] = 2;
dfs(id + 1, left - 2);
place[id] = 0;
}
}
int main() {
scanf("%d%d%d", &nf, &ne, &ns);
scanf("%d%d%d", &rf, &re, &rs);
scanf("%d%d", &df, &de);
lf = sqrt(rf * rf - 1);
le = sqrt(re * re - 1);
ls = sqrt(rs * rs - 1);
for (int i = 1; i <= 13; i++)
for (int j = 1; j <= 13; j++)
interFLen[i][j] = inter(i - lf, i + lf, j - ls, j + ls),
interELen[i][j] = inter(i - le, i + le, j - ls, j + ls);
dfs(1, ns);
cout << fixed << setprecision(6) << ans << endl;
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define N 305
#define pi pair<int,int>
#define mk make_pair
#define Mod 1000000007
using namespace std;
int n,m,x,y,z,dp[N][N][N];
vector<pi>G[N];
inline bool check(int x,int y,int z){
int pos=max(x,max(y,z));
if (x<y) swap(x,y);if (x<z) swap(x,z);
if (y<z) swap(y,z);
for (int i=0;i<(int)G[pos].size();i++){
int l=G[pos][i].first;
int limit=G[pos][i].second;
if (limit==3) if (z<l) return 0;
if (limit==2){
if (y<l) return 0;
if (z>=l) return 0;
}
if (limit==1) if (y>=l) return 0;
}
return 1;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
G[y].push_back(mk(x,z));
}
dp[0][0][0]=1;
int ans=0;
for (int i=0;i<=n;i++){
for (int j=0;j<=n;j++){
for (int k=0;k<=n;k++){
if (!dp[i][j][k]) continue;
int nxt=max(i,max(j,k))+1;
if (nxt==n+1){
ans=(ans+dp[i][j][k])%Mod;
continue;
}
if (check(nxt,j,k)) dp[nxt][j][k]=1ll*(dp[nxt][j][k]+dp[i][j][k])%Mod;
if (check(i,nxt,k)) dp[i][nxt][k]=1ll*(dp[i][nxt][k]+dp[i][j][k])%Mod;
if (check(i,j,nxt)) dp[i][j][nxt]=1ll*(dp[i][j][nxt]+dp[i][j][k])%Mod;
}
}
}
printf("%d\n",ans);
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int A;
string S;
set<string> st;
cin>>A;
for(int i = 0;i < A;i++){
cin>>S;
st.insert(S);
}
cout<<st.size();
} | 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
#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);}
const ll MOD=1e9+7; //998244353
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cout<<fixed<<setprecision(0);
cauto T=read();
rep(_,T){
cauto n=read();
cauto vs=readV(n);
ll re=0;
reps(i,1,n) re+=abs(vs[i]-vs[i-1]);
ll mx=max(abs(vs[0]-vs[1]),abs(vs[n-1]-vs[n-2]));
reps(i,1,n-1){
cauto tmp=abs(vs[i]-vs[i-1])+abs(vs[i]-vs[i+1]);
MX(mx,tmp-abs(vs[i+1]-vs[i-1]));
out(tmp,tmp-abs(vs[i+1]-vs[i-1]),1);
}
out(mx,re,1);
cout<<re-mx NL;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int _max(int x, int y) { return x > y ? x : y; }
const long long mod = 1e9 + 7;
int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
long long inv[1000010], jc[1000010];
long long f[1000010], sum[1000010];
long long pow_mod(long long a, long long k) {
long long ans = 1;
while (k) {
if (k & 1) (ans *= a) %= mod;
(a *= a) %= mod;
k /= 2;
}
return ans;
}
long long C(long long n, long long m) {
return jc[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
int n = read(), k = read();
jc[0] = 1;
for (int i = 1; i <= n; i++) jc[i] = (long long)jc[i - 1] * i % mod;
inv[0] = 1;
inv[n] = pow_mod(jc[n], mod - 2);
for (int i = n - 1; i >= 1; i--)
inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
sum[1] = 1;
f[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = (sum[i] - sum[_max(0, i - k)]) % mod * jc[i - 1] % mod;
(sum[i + 1] = sum[i] + f[i] * inv[i] % mod) % mod;
}
long long ans = 0;
for (int i = 1; i <= n; i++)
(ans += f[i - 1] * C(n - 1, i - 1) % mod * jc[n - i] % mod) % mod;
printf("%lld\n", ((jc[n] - ans) % mod + mod) % mod);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int y, w, next;
} path[100005 * 2];
int first[100005], p;
inline void add(int x, int y, int w) {
path[p].y = y;
path[p].w = w;
path[p].next = first[x];
first[x] = p++;
}
int fa[100005][18], dep[100005], bg[100005], ed[100005], idx;
int stn[100005];
void dfs(int x, int r, int d) {
fa[x][0] = r;
dep[x] = d;
for (int i = 1; i < 18; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
int v;
bg[x] = ++idx;
for (int i = first[x]; i != -1; i = path[i].next) {
v = path[i].y;
if (v == r) continue;
stn[path[i].w] = v;
dfs(v, x, d + 1);
}
ed[x] = idx;
}
int n;
int d[100005 * 2];
inline int lowbit(int x) { return x & (-x); }
void update(int x, int data) {
for (int i = x; i <= idx; i += lowbit(i)) d[i] += data;
}
int query(int x) {
int ret = 0;
for (int i = x; i > 0; i -= lowbit(i)) ret += d[i];
return ret;
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int d = dep[y] - dep[x];
for (int i = 17; i >= 0; i--)
if (d & (1 << i)) y = fa[y][i];
if (x == y) return y;
for (int i = 17; i >= 0; i--)
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
return fa[x][0];
}
void solve(int x, int y) {
int fa = lca(x, y);
int s1 = query(bg[x]) - query(bg[fa]);
int s2 = query(bg[y]) - query(bg[fa]);
if (s1 != dep[x] - dep[fa] || s2 != dep[y] - dep[fa])
printf("-1\n");
else
printf("%d\n", dep[x] - dep[fa] + dep[y] - dep[fa]);
}
int state[100005];
int main() {
int x, y, f;
while (scanf("%d", &n) != EOF) {
memset(first, -1, sizeof(first));
p = 0;
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
add(x, y, i);
add(y, x, i);
}
idx = 0;
dfs(1, 0, 0);
memset(d, 0, sizeof(d));
for (int i = 1; i < n; i++) {
update(bg[stn[i]], 1);
update(ed[stn[i]] + 1, -1);
state[i] = 1;
}
int m;
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d", &f);
if (f == 1) {
scanf("%d", &x);
if (state[x] == 0) {
state[x] = 1;
update(bg[stn[x]], 1);
update(ed[stn[x]] + 1, -1);
}
} else if (f == 2) {
scanf("%d", &x);
if (state[x] == 1) {
state[x] = 0;
update(bg[stn[x]], -1);
update(ed[stn[x]] + 1, 1);
}
} else {
scanf("%d %d", &x, &y);
solve(x, y);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<long long, long long>>> adj;
vector<unordered_set<long long>> tra;
long long n;
long long m;
long long dijkstra() {
long long source = 1;
long long destination = n;
long long var[n + 1];
long long fix[n + 1];
for (long long i = 1; i <= n; i++) {
var[i] = 1e18;
}
memset(fix, -1, sizeof(fix));
set<pair<long long, long long>> s;
s.insert({0, source});
fix[source] = 0;
while (s.size()) {
pair<long long, long long> cur = *(s.begin());
s.erase(cur);
fix[cur.second] = cur.first;
long long leave = 0;
while (tra[cur.second].count(cur.first + leave)) {
leave++;
}
for (auto x : adj[cur.second]) {
long long check = cur.first + x.second;
if (var[x.first] > cur.first + x.second + leave) {
s.erase({var[x.first], x.first});
var[x.first] = cur.first + x.second + leave;
s.insert({var[x.first], x.first});
}
}
}
return fix[destination];
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
cin >> m;
adj.resize(n + 1);
tra.resize(n + 1);
for (long long i = 0; i < m; i++) {
long long a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
for (long long i = 1; i <= n; i++) {
long long siz;
cin >> siz;
for (long long j = 0; j < siz; j++) {
long long x;
cin >> x;
tra[i].insert(x);
}
}
cout << dijkstra();
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int N = 1e6 + 5, M = 1e6 + 5;
int n, m, tp, st[N];
struct Edge {
int u, v, w;
bool operator<(const Edge &b) const { return w < b.w; }
} e[M];
struct Tree {
int fa, ch[2], mx, tag, v;
} t[N];
void pushdown(int rt) {
if (!t[rt].tag) return;
t[rt].tag = 0;
std::swap(t[rt].ch[0], t[rt].ch[1]);
t[t[rt].ch[0]].tag ^= 1;
t[t[rt].ch[1]].tag ^= 1;
}
void update(int rt) {
t[rt].mx = t[rt].v > t[t[t[rt].ch[0]].mx].v ? rt : t[t[rt].ch[0]].mx;
t[rt].mx =
t[t[rt].mx].v > t[t[t[rt].ch[1]].mx].v ? t[rt].mx : t[t[rt].ch[1]].mx;
}
bool isroot(int rt) {
return rt != t[t[rt].fa].ch[0] && rt != t[t[rt].fa].ch[1];
}
void rot(int rt) {
int fa = t[rt].fa, gfa = t[fa].fa;
int t1 = (rt != t[fa].ch[0]), t2 = (fa != t[gfa].ch[0]),
ch = t[rt].ch[1 ^ t1];
if (!isroot(fa)) t[gfa].ch[t2] = rt;
t[ch].fa = fa, t[rt].fa = gfa, t[rt].ch[t1 ^ 1] = fa, t[fa].fa = rt,
t[fa].ch[t1] = ch;
update(fa);
}
void splay(int rt) {
st[++tp] = rt;
for (int i = rt; !isroot(i); i = t[i].fa) st[++tp] = t[i].fa;
while (tp) pushdown(st[tp--]);
for (; !isroot(rt); rot(rt)) {
int fa = t[rt].fa, gfa = t[fa].fa;
if (!isroot(fa))
(rt == t[fa].ch[0]) ^ (fa == t[gfa].ch[0]) ? rot(rt) : rot(fa);
}
update(rt);
}
void access(int rt) {
int tmp = 0;
while (rt) splay(rt), t[rt].ch[1] = tmp, update(rt), tmp = rt, rt = t[rt].fa;
}
void changeroot(int rt) { access(rt), splay(rt), t[rt].tag ^= 1; }
void link(int x, int y) { changeroot(x), t[x].fa = y; }
void cut(int x, int y) {
changeroot(x), access(y), splay(y), t[y].ch[0] = t[x].fa = 0;
}
int findroot(int rt) {
access(rt), splay(rt);
int tmp = rt;
while (t[tmp].ch[0]) tmp = t[tmp].ch[0];
return tmp;
}
void work(int x, int y) { changeroot(x), access(y), splay(y); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
std::sort(e + 1, e + 1 + m);
for (int i = 1; i <= m; ++i) t[n + i].v = e[i].w, t[n + i].mx = n + i;
int ans = 0;
for (int i = 1; i <= m; ++i) {
if (findroot(e[i].u) != findroot(e[i].v))
link(e[i].u, n + i), link(n + i, e[i].v);
else
work(e[i].u, e[i].v), update(e[i].v),
ans += (e[i].w == t[t[e[i].v].mx].v);
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int l, r, c;
long long m2, m3;
c = 0;
m2 = 1;
scanf("%d %d", &l, &r);
while (m2 <= r) {
m3 = 1;
while (m2 * m3 <= r) {
if (m2 * m3 >= l) {
c += 1;
}
m3 *= 3;
}
m2 *= 2;
}
printf("%d", c);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
cin >> a;
int n = a.size();
int start = 0;
int ans = 0;
for (int i = 0; i < n - 3; i++) {
if (a.substr(i, 4) == "bear") {
ans += (i - start + 1) * (n - i - 3);
start = i + 1;
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> arr(n);
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mp[arr[i]]++;
}
if (mp.size() < k) {
cout << -1 << ' ' << -1 << endl;
} else {
int l = 0, r = n - 1;
int ans = mp.size();
while (ans >= k) {
mp[arr[r]]--;
if (mp[arr[r]] == 0) {
ans--;
}
r--;
}
r++;
ans++;
while (ans >= k && l <= r) {
mp[arr[l]]--;
if (mp[arr[l]] == 0) {
ans--;
}
l++;
}
l--;
cout << l + 1 << ' ' << r + 1 << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define f first
#define s second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
const int SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(SEED);
string p = "haha";
struct item{
string pref, suff;
ll ocur = 0, sz = 0;
item(){}
item(string pref, string suff, ll sz, ll ocur):pref(pref), suff(suff), sz(sz), ocur(ocur){}
item(string str){
int n = str.size();
for(int i = 0; i + 3 < n; i++){
bool found = true;
for(int j = 0; j < 4; j++)
if(str[i+j] != p[j])found = false;
if(found)ocur++;
}
sz = n;
if(sz < 4)pref = suff = str;
else{
pref = str.substr(0, 3);
suff = str.substr(n-3, 3);
}
}
item operator+(const item rhs)const{
ll sz1 = sz + rhs.sz;
ll ocur1 = ocur + rhs.ocur;
string pref1 = pref + rhs.pref, suff1 = suff + rhs.suff;
string mid = suff + rhs.pref;
for(int i = 0; i < (int)suff.size(); i++){
bool found = true;
for(int j = 0; j < 4; j++)
if(mid[i+j] != p[j])found = false;
if(found)ocur1++;
}
int n = pref1.size();
if(n >= 4)pref1 = pref1.substr(0, 3);
int m = suff1.size();
if(m >= 4)suff1 = suff1.substr(m-3, 3);
return item(pref1, suff1, sz1, ocur1);
return item(pref);
};
};
void test_case(){
int n;
cin >> n;
string a, b, c, op;
map<string, item> var;
cin.ignore();
while(n--){
string r;
getline(cin, r);
stringstream ss(r);
ss >> a >> op;
if(op == ":="){
ss >> b;
var[a] = item(b);
}
else{
ss >> b >> op >> c;
var[a] = var[b] + var[c];
}
}
cout << var[a].ocur << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
//cout << "Case #"<< i << ":" << endl;
test_case();
}
return 0;
} | 5 |
#include <iostream>
using namespace std;
int main() {
int b[3];
for (int i = 0; i < 3; ++i) cin >> b[i];
if ((b[0] & b[1]) || b[2]) cout << "Open" << endl;
else cout << "Close" <<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long x, y, z, i, j, n, m, k, s1, s2, s3, s4, s5, s6, mod, l, r, t, maxx,
minn, sx, so, sum, ans, puk;
double u, v, w, x1, x2, x3, x4, z1, z2, z3, h, huh;
string s;
string q;
string p;
string sq[145];
long long a[1000045];
long long b[1000045];
long long c[1000045];
long long d[1000045];
double dp[145][145][145];
vector<long long> vc;
vector<long long> cv;
pair<long long, long long> aa[2045];
pair<long long, long long> pr;
stack<long long> st;
stack<long long> ts;
map<long long, long long> mp;
int main() {
scanf("%lld", &n);
x1 = 0.0;
for (i = 1; i <= n; i++) {
scanf("%lf", &u);
x1 = max(u, x1);
}
scanf("%lld", &n);
x2 = 0.0;
for (i = 1; i <= n; i++) {
scanf("%lf", &u);
x2 = max(u, x2);
}
scanf("%lld", &n);
x3 = 1234567890.0;
for (i = 1; i <= n; i++) {
scanf("%lf", &u);
x3 = min(u, x3);
}
scanf("%lf%lf", &u, &v);
w = x1 * sqrt(x2 * v / (x2 * v + x3 * u));
printf("%.12lf", w);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, j, i;
string str, str1, str2;
cin >> n;
for (i = 0; i < n; i++) {
cin >> str;
if (str.length() % 2 == 0) {
j = str.length() / 2;
str1 = str.substr(0, j);
str2 = str.substr(j, j);
if (str1 == str2) {
cout << "YES" << endl;
} else
cout << "NO" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rep2(i,k,n) for(int i=k;i>=n;i--)
using namespace std;
const int N=5e5+7;
char s[N];
int a[N],n,now;
int main(){
scanf("%s",s+1);
n=strlen(s+1);
rep(i,1,n)a[i+1]=s[i]-'0';
int c=0;
rep2(i,n+1,1){
int res=a[i]*9 + c;
a[i]=res%10;
c=res/10;
now+=a[i];
}
rep(k,1,1000000){
c=9;
rep2(i,n+1,0){
if(a[i]+c < 10){
now+=c;
a[i]+=c;
break;
}else{
now-=a[i];
int res=a[i]+c;
c=res/10;
a[i]=res%10;
now+=a[i];
}
}
if(now<=9*k){
printf("%d\n",k);
return 0;
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename U, typename V>
string to_string(pair<U, V>);
string to_string(const string& e_) { return "\"" + e_ + "\""; }
string to_string(char e_) { return "\'" + string(1, e_) + "\'"; }
string to_string(bool e_) { return e_ ? "true" : "false"; }
template <typename T>
string to_string(T e_) {
string s_ = "[ ";
for (const auto& x_ : e_) s_ += to_string(x_) + " ";
return s_ + "]";
}
template <typename U, typename V>
string to_string(pair<U, V> e_) {
return "(" + to_string(e_.first) + ", " + to_string(e_.second) + ")";
}
void dbg_str() { ; }
template <typename U, typename... V>
void dbg_str(U u, V... v) {
;
dbg_str(v...);
}
vector<pll> entry, inter, tmp;
void process(ll k, ll t) {
tmp.clear();
ll itr = min(k, (ll)inter.size());
for (int i = 0; i < itr; i++)
tmp.push_back({inter[i].first, inter[i].second + t});
for (int i = itr; i < (int)inter.size(); i++)
tmp.push_back(
{inter[i].first, max(tmp[i - itr].second, inter[i].second) + t});
inter = tmp;
;
}
int main() {
int n, k[3], t[3], c;
for (int i = 0; i < 3; i++) scanf("%d", &k[i]);
for (int i = 0; i < 3; i++) scanf("%d", &t[i]);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &c);
entry.push_back({i, c});
};
inter = entry;
for (int i = 0; i < 3; i++) process(k[i], t[i]);
ll mx = 0;
for (int i = 0; i < (int)inter.size(); i++)
mx = max(mx, inter[i].second - entry[i].second);
printf("%lld\n", mx);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
const ll mod = 2;
struct Mod
{
ll num;
Mod() : num(0) { ; }
Mod(ll n) : num(n % mod) { ; }
operator ll() { return num; }
};
Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
typedef Mod Data;
typedef vector<Data> Array;
typedef vector<Array> Matrix;
bool is_zero(Data dat) { return (abs(dat) < EPS); }
Matrix operator-(Matrix mat) {
REP(i, mat.size()) REP(j, mat[0].size()) mat[i][j] = -mat[i][j];
return mat;
}
Matrix operator+(Matrix lhs, const Matrix &rhs) {
REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] + rhs[i][j];
return lhs;
}
Matrix operator-(Matrix lhs, const Matrix &rhs) {
REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] - rhs[i][j];
return lhs;
}
Matrix operator*(const Matrix &lhs, const Matrix &rhs) {
Matrix res(lhs.size(), Array(rhs[0].size(), 0));
REP(i, lhs.size()) REP(j, rhs[0].size()) REP(k, rhs.size())
res[i][j] = lhs[i][k] * rhs[k][j] + res[i][j];
return res;
}
Array operator+(const Array &lhs, const Array &rhs) {
Array res = lhs;
REP(i, rhs.size()) res[i] = res[i] + rhs[i];
return res;
}
Array operator*(const Matrix &lhs, const Array &rhs) {
Array res(lhs.size());
REP(i, lhs.size())
res[i] = inner_product(begin(lhs[i]), end(lhs[i]), begin(rhs), Data(0));
return res;
}
Array operator*(Data scalar, const Array &rhs) {
Array res(rhs.size());
REP(i, rhs.size())
res[i] = scalar * rhs[i];
return res;
}
Matrix scalar(int size, Data k) {
Matrix mat(size, Array(size, 0));
REP(i, size) mat[i][i] = k;
return mat;
}
Matrix operator^(const Matrix &lhs, const int n) {
if (n == 0) return scalar(lhs.size(), 1);
Matrix res = (lhs * lhs) ^ (n / 2);
if (n % 2) res = res * lhs;
return res;
}
int rankMat(Matrix A) {
const int n = A.size(), m = A[0].size();
int r = 0;
for (int i = 0; r < n && i < m; ++i) {
int pivot = r;
for (int j = r + 1; j < n; ++j)
if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;
swap(A[pivot], A[r]);
if (is_zero(A[r][i])) continue;
for (int k = m - 1; k >= i; --k)
A[r][k] = A[r][k] / A[r][i];
for (int j = r + 1; j < n; ++j)
for (int k = m - 1; k >= i; --k)
A[j][k] = (Data)-A[r][k] * A[j][i] + A[j][k];
++r;
}
return r;
}
Array gauss_jordan(const Matrix& A, const Array& b)
{
int n = A.size();
Matrix B(n, Array(n + 1));
REP(i, n)REP(j, n) B[i][j] = A[i][j];
REP(i, n) B[i][n] = b[i];
REP(i, n)
{
int pivot = i;
FOR(j, i, n)
{
if (abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS) return Array();
FOR(j, i + 1, n + 1) B[i][j] = B[i][j] / B[i][i];
REP(j, n)
{
if (i != j)
{
FOR(k, i + 1, n + 1) B[j][k] -= B[j][i] * B[i][k];
}
}
}
Array x(n);
REP(i, n) x[i] = B[i][n];
return x;
}
int main()
{
cin.sync_with_stdio(false); cout << fixed << setprecision(10);
int n;
cin >> n;
Matrix A_(n, Array(n));
REP(i, n)REP(j, n)
{
int a; cin >> a;
A_[i][j] = Data(a);
}
Array b(n);
REP(i, n)
{
int a; cin >> a;
b[i] = Data(a);
}
int t; cin >> t;
Matrix A = A_^t;
Matrix Ab = A;
REP(i, n) Ab[i].push_back(b[i]);
int rA = rankMat(A), rAb = rankMat(Ab);
if (rA != rAb)
{
cout << "none" << endl;
return 0;
}
if (rA == rAb && rA != n)
{
cout << "ambiguous" << endl;
return 0;
}
Array ans = gauss_jordan(A, b);
REP(i, n) cout << ans[i] << (i == n - 1 ? "\n" : " ");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a = 0, b = 0, c = 0, arra[1000], arrb[1000], arrc[1000];
bool h = true, f = true;
char hin;
string hh1, hh2, hh3;
getline(cin, hh1);
getline(cin, hh2);
getline(cin, hh3);
for (int i = 0; hh1[i]; i++) {
if (hh1[i] == 'a' || hh1[i] == 'e' || hh1[i] == 'i' || hh1[i] == 'o' ||
hh1[i] == 'u') {
a++;
}
}
for (int i = 0; hh2[i]; i++) {
if (hh2[i] == 'a' || hh2[i] == 'e' || hh2[i] == 'i' || hh2[i] == 'o' ||
hh2[i] == 'u') {
b++;
}
}
for (int i = 0; hh3[i]; i++) {
if (hh3[i] == 'a' || hh3[i] == 'e' || hh3[i] == 'i' || hh3[i] == 'o' ||
hh3[i] == 'u') {
c++;
}
}
if (a == 5 && b == 7 && c == 5) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O2")
const int LIM = 5e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<int> books(n + 1, -1);
int curr_idx = 0;
for (int i = 0; i < n; i++) {
int book;
cin >> book;
if (books[book] > 0) {
cout << "0 ";
continue;
}
int idx;
for (int j = curr_idx; j < n; j++) {
books[arr[j]] = 1;
if (arr[j] == book) {
idx = j;
break;
}
}
cout << (idx - curr_idx + 1) << " ";
curr_idx = idx + 1;
}
}
| 2 |
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main(){
int ans[100];
queue<int> row;
int t = 0, cur = -1;
vector<int> in(17, -1);
while(cur != 99){
// new customer
if(t%5 == 0 && t < 500) row.push(t/5);
// customer leaves
for(int i = 0; i < 17; i++) if(t >= in[i]) in[i] = -1;
// customer comes in
while(!row.empty()){
int next = row.front();
int num = next%5==1 ? 5 : 2;
int i;
for(i = 0; i <= 17-num; i++){
int vacant = 0;
for(int j = 0; j < num; j++){
vacant += (in[i+j]==-1);
}
if(vacant == num){
break;
}
}
if(i > 17-num) break;
// decide the position i
row.pop();
int tot = t + 17*(next%2)+3*(next%3)+19;
for(int j = 0; j < num; j++){
in[i+j] = tot;
}
ans[next] = t-next*5;
cur = next;
}
// time passes
t++;
}
int n;
while(cin >> n) cout << ans[n] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
const long double 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;
}
void solve() {
int a, b, k, t;
cin >> a >> b >> k >> t;
vector<vector<long long>> dp(t + 1, vector<long long>(2 * k * t + 2));
dp[0][k * t] = 1;
for (int i = (int)(0); i < (int)(t); i++) {
for (int j = (int)(0); j < (int)(2 * k * t + 1); j++) {
(dp[i + 1][max(0, j - k)] += dp[i][j]) %= mod;
(dp[i + 1][min(2 * k * t, j + k) + 1] += mod - dp[i][j]) %= mod;
}
for (int j = (int)(0); j < (int)(2 * k * t + 1); j++) {
if (j) (dp[i + 1][j] += mod + dp[i + 1][j - 1]) %= mod;
}
}
vector<long long> sum(2 * k * t + 2);
for (int i = (int)((2 * k * t + 1) - 1); i >= (int)(0); i--) {
(sum[i] = sum[i + 1] + dp[t][i]) %= mod;
}
long long ans = 0;
for (int i = (int)(0); i < (int)(2 * k * t + 1); i++) {
int need = b + i - a + 1;
if (need >= 2 * k * t + 2) continue;
(ans += dp[t][i] * sum[max(0, need)] % mod) %= mod;
}
cout << ans << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T = 1;
while (T--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[300][300], n, k;
int main() {
cin >> n >> k;
a[1][1] = 0;
a[2][1] = 2;
a[2][2] = 1;
a[4][1] = 32;
a[4][2] = 30;
a[4][3] = 80;
a[4][4] = 109;
a[8][1] = 6824;
a[8][2] = 59808;
a[8][3] = 147224;
a[8][4] = 415870;
a[8][5] = 1757896;
a[8][6] = 1897056;
a[8][7] = 4898872;
a[8][8] = 7593125;
a[16][1] = 776830421;
a[16][2] = 290516100;
a[16][3] = 746623577;
a[16][4] = 293783147;
a[16][5] = 33900006;
a[16][6] = 735127505;
a[16][7] = 565460332;
a[16][8] = 428982705;
a[16][9] = 472062098;
a[16][10] = 161873957;
a[16][11] = 117354594;
a[16][12] = 515619293;
a[16][13] = 578944191;
a[16][14] = 312106242;
a[16][15] = 569389279;
a[16][16] = 391464593;
a[32][1] = 261086313;
a[32][2] = 584837659;
a[32][3] = 683961846;
a[32][4] = 468868529;
a[32][5] = 211593382;
a[32][6] = 736955478;
a[32][7] = 229471758;
a[32][8] = 157617135;
a[32][9] = 398169441;
a[32][10] = 360252438;
a[32][11] = 629394768;
a[32][12] = 264125799;
a[32][13] = 647490480;
a[32][14] = 342079395;
a[32][15] = 391579767;
a[32][16] = 225200475;
a[32][17] = 486011304;
a[32][18] = 513156108;
a[32][19] = 628771752;
a[32][20] = 132906648;
a[32][21] = 142138221;
a[32][22] = 20119449;
a[32][23] = 444199674;
a[32][24] = 195188679;
a[32][25] = 387329805;
a[32][26] = 44684703;
a[32][27] = 651912135;
a[32][28] = 737154512;
a[32][29] = 612549793;
a[32][30] = 519860281;
a[32][31] = 186175544;
a[32][32] = 212568440;
a[64][1] = 240805271;
a[64][2] = 239509872;
a[64][3] = 581127897;
a[64][4] = 6511239;
a[64][5] = 156126222;
a[64][6] = 509425833;
a[64][7] = 672407328;
a[64][8] = 366667722;
a[64][9] = 459185405;
a[64][10] = 509737025;
a[64][11] = 554790222;
a[64][12] = 165216555;
a[64][13] = 703150560;
a[64][14] = 74806569;
a[64][15] = 398730015;
a[64][16] = 383350905;
a[64][17] = 506108358;
a[64][18] = 51326142;
a[64][19] = 298053147;
a[64][20] = 104256117;
a[64][21] = 391428765;
a[64][22] = 374020479;
a[64][23] = 206607807;
a[64][24] = 87664059;
a[64][25] = 275899176;
a[64][26] = 56407680;
a[64][27] = 551553401;
a[64][28] = 448939463;
a[64][29] = 582889860;
a[64][30] = 129676638;
a[64][31] = 226078251;
a[64][32] = 135769095;
a[64][33] = 61292868;
a[64][34] = 578972226;
a[64][35] = 190181628;
a[64][36] = 390739055;
a[64][37] = 184587732;
a[64][38] = 446575689;
a[64][39] = 732674124;
a[64][40] = 232198470;
a[64][41] = 676760679;
a[64][42] = 352474101;
a[64][43] = 611444862;
a[64][44] = 575661807;
a[64][45] = 628905585;
a[64][46] = 320813094;
a[64][47] = 522840969;
a[64][48] = 469781928;
a[64][49] = 156006018;
a[64][50] = 554473341;
a[64][51] = 239654268;
a[64][52] = 643714911;
a[64][53] = 433540170;
a[64][54] = 199307003;
a[64][55] = 496385218;
a[64][56] = 291740751;
a[64][57] = 67309914;
a[64][58] = 370826673;
a[64][59] = 202356819;
a[64][60] = 279421821;
a[64][61] = 421203111;
a[64][62] = 63744786;
a[64][63] = 520987612;
a[64][64] = 550671827;
a[128][1] = 482164403;
a[128][2] = 768209115;
a[128][3] = 462063756;
a[128][4] = 154906374;
a[128][5] = 36099042;
a[128][6] = 341766705;
a[128][7] = 678182556;
a[128][8] = 621882744;
a[128][9] = 478771358;
a[128][10] = 231881111;
a[128][11] = 175889805;
a[128][12] = 243630450;
a[128][13] = 168908523;
a[128][14] = 671961765;
a[128][15] = 55761813;
a[128][16] = 652682670;
a[128][17] = 773939082;
a[128][18] = 517628076;
a[128][19] = 756201264;
a[128][20] = 124604900;
a[128][21] = 750976272;
a[128][22] = 498253248;
a[128][23] = 676047609;
a[128][24] = 137170026;
a[128][25] = 705610017;
a[128][26] = 495032139;
a[128][27] = 561797418;
a[128][28] = 703097347;
a[128][29] = 500815609;
a[128][30] = 95984586;
a[128][31] = 739707108;
a[128][32] = 265613565;
a[128][33] = 387099846;
a[128][34] = 777331779;
a[128][35] = 594676173;
a[128][36] = 591219559;
a[128][37] = 407997044;
a[128][38] = 208947235;
a[128][39] = 93337440;
a[128][40] = 478908360;
a[128][41] = 685013007;
a[128][42] = 487033953;
a[128][43] = 671903001;
a[128][44] = 39521181;
a[128][45] = 738490312;
a[128][46] = 33785059;
a[128][47] = 465470131;
a[128][48] = 310453920;
a[128][49] = 54648783;
a[128][50] = 346831137;
a[128][51] = 427694175;
a[128][52] = 474743430;
a[128][53] = 705296781;
a[128][54] = 435828036;
a[128][55] = 429824745;
a[128][56] = 663532359;
a[128][57] = 261388683;
a[128][58] = 244690731;
a[128][59] = 533997135;
a[128][60] = 596108961;
a[128][61] = 506813013;
a[128][62] = 371892402;
a[128][63] = 590145264;
a[128][64] = 104733162;
a[128][65] = 143420103;
a[128][66] = 654339672;
a[128][67] = 700348950;
a[128][68] = 685038942;
a[128][69] = 578826927;
a[128][70] = 286484229;
a[128][71] = 501639192;
a[128][72] = 434962491;
a[128][73] = 299270097;
a[128][74] = 27702486;
a[128][75] = 335375775;
a[128][76] = 111746817;
a[128][77] = 565603164;
a[128][78] = 294926121;
a[128][79] = 676063665;
a[128][80] = 735862995;
a[128][81] = 710035809;
a[128][82] = 437011960;
a[128][83] = 668528077;
a[128][84] = 138765186;
a[128][85] = 508213986;
a[128][86] = 615036450;
a[128][87] = 353784942;
a[128][88] = 624827616;
a[128][89] = 343900011;
a[128][90] = 241289776;
a[128][91] = 52410890;
a[128][92] = 72018835;
a[128][93] = 352406796;
a[128][94] = 415705878;
a[128][95] = 4802637;
a[128][96] = 376367145;
a[128][97] = 65589678;
a[128][98] = 333633477;
a[128][99] = 341834527;
a[128][100] = 303717460;
a[128][101] = 282387700;
a[128][102] = 42951006;
a[128][103] = 254706039;
a[128][104] = 423048528;
a[128][105] = 526429710;
a[128][106] = 68131467;
a[128][107] = 669954708;
a[128][108] = 12787348;
a[128][109] = 500636381;
a[128][110] = 317959019;
a[128][111] = 479433192;
a[128][112] = 657133515;
a[128][113] = 416259390;
a[128][114] = 610216692;
a[128][115] = 340129188;
a[128][116] = 44594256;
a[128][117] = 257373347;
a[128][118] = 138718678;
a[128][119] = 530767740;
a[128][120] = 292922628;
a[128][121] = 37220268;
a[128][122] = 605295159;
a[128][123] = 480722613;
a[128][124] = 458170419;
a[128][125] = 30540300;
a[128][126] = 487159055;
a[128][127] = 232966794;
a[128][128] = 149150650;
a[256][1] = 412133651;
a[256][2] = 386543325;
a[256][3] = 139952108;
a[256][4] = 289303402;
a[256][5] = 102404925;
a[256][6] = 317067177;
a[256][7] = 396414708;
a[256][8] = 80515854;
a[256][9] = 663739304;
a[256][10] = 317300809;
a[256][11] = 228877044;
a[256][12] = 493725043;
a[256][13] = 715317967;
a[256][14] = 490300965;
a[256][15] = 315527373;
a[256][16] = 743539734;
a[256][17] = 488329191;
a[256][18] = 553627998;
a[256][19] = 533025234;
a[256][20] = 242583957;
a[256][21] = 706116537;
a[256][22] = 614109258;
a[256][23] = 645447222;
a[256][24] = 523195911;
a[256][25] = 492109128;
a[256][26] = 722623041;
a[256][27] = 111085128;
a[256][28] = 766395126;
a[256][29] = 654378921;
a[256][30] = 691964847;
a[256][31] = 496688157;
a[256][32] = 399056049;
a[256][33] = 654363234;
a[256][34] = 102052314;
a[256][35] = 191720088;
a[256][36] = 473910948;
a[256][37] = 259736526;
a[256][38] = 332840025;
a[256][39] = 388047555;
a[256][40] = 665791056;
a[256][41] = 627111387;
a[256][42] = 139696515;
a[256][43] = 441456687;
a[256][44] = 443032569;
a[256][45] = 283264821;
a[256][46] = 771641703;
a[256][47] = 452641455;
a[256][48] = 511306362;
a[256][49] = 117572859;
a[256][50] = 127701891;
a[256][51] = 721298331;
a[256][52] = 176520078;
a[256][53] = 357242229;
a[256][54] = 611296308;
a[256][55] = 696994956;
a[256][56] = 405628839;
a[256][57] = 429224274;
a[256][58] = 465336054;
a[256][59] = 695091546;
a[256][60] = 689828796;
a[256][61] = 574648641;
a[256][62] = 351220905;
a[256][63] = 507964023;
a[256][64] = 675326610;
a[256][65] = 517248963;
a[256][66] = 453528621;
a[256][67] = 220301928;
a[256][68] = 494463186;
a[256][69] = 681789969;
a[256][70] = 339589656;
a[256][71] = 44524053;
a[256][72] = 417125457;
a[256][73] = 339589404;
a[256][74] = 747135963;
a[256][75] = 341780733;
a[256][76] = 734158215;
a[256][77] = 396817281;
a[256][78] = 21997836;
a[256][79] = 5728464;
a[256][80] = 147611205;
a[256][81] = 456248898;
a[256][82] = 714128667;
a[256][83] = 377654949;
a[256][84] = 3862068;
a[256][85] = 128418948;
a[256][86] = 589390074;
a[256][87] = 304947090;
a[256][88] = 11703825;
a[256][89] = 228266073;
a[256][90] = 127304142;
a[256][91] = 429215724;
a[256][92] = 361541124;
a[256][93] = 521572968;
a[256][94] = 468358191;
a[256][95] = 341231688;
a[256][96] = 65323503;
a[256][97] = 613778508;
a[256][98] = 15985323;
a[256][99] = 291661029;
a[256][100] = 410970006;
a[256][101] = 591638112;
a[256][102] = 349541550;
a[256][103] = 89967528;
a[256][104] = 224922159;
a[256][105] = 361094166;
a[256][106] = 584206074;
a[256][107] = 640051812;
a[256][108] = 324264456;
a[256][109] = 652625388;
a[256][110] = 693768537;
a[256][111] = 11740617;
a[256][112] = 309238398;
a[256][113] = 211085469;
a[256][114] = 194905872;
a[256][115] = 639416484;
a[256][116] = 110110707;
a[256][117] = 296645895;
a[256][118] = 748118511;
a[256][119] = 131177718;
a[256][120] = 511142751;
a[256][121] = 775975599;
a[256][122] = 421403409;
a[256][123] = 475528473;
a[256][124] = 434685258;
a[256][125] = 1768977;
a[256][126] = 80301375;
a[256][127] = 708023862;
a[256][128] = 569195676;
a[256][129] = 56238084;
a[256][130] = 632887668;
a[256][131] = 88089750;
a[256][132] = 631539342;
a[256][133] = 396695565;
a[256][134] = 38780154;
a[256][135] = 695798271;
a[256][136] = 469819224;
a[256][137] = 439587099;
a[256][138] = 69045921;
a[256][139] = 682966116;
a[256][140] = 112310856;
a[256][141] = 64943298;
a[256][142] = 534475872;
a[256][143] = 40215357;
a[256][144] = 389728458;
a[256][145] = 286368453;
a[256][146] = 736006257;
a[256][147] = 501181650;
a[256][148] = 54829908;
a[256][149] = 603489402;
a[256][150] = 338032656;
a[256][151] = 512182818;
a[256][152] = 627500097;
a[256][153] = 462674016;
a[256][154] = 3103092;
a[256][155] = 157324491;
a[256][156] = 43978329;
a[256][157] = 596818971;
a[256][158] = 259025598;
a[256][159] = 9088632;
a[256][160] = 91991781;
a[256][161] = 577291428;
a[256][162] = 211245489;
a[256][163] = 429471231;
a[256][164] = 142626330;
a[256][165] = 172560633;
a[256][166] = 510907446;
a[256][167] = 444609585;
a[256][168] = 758102058;
a[256][169] = 375112647;
a[256][170] = 744786693;
a[256][171] = 276174402;
a[256][172] = 19259856;
a[256][173] = 233672418;
a[256][174] = 745389414;
a[256][175] = 225772848;
a[256][176] = 23385663;
a[256][177] = 324290610;
a[256][178] = 519804558;
a[256][179] = 120337812;
a[256][180] = 402578568;
a[256][181] = 360676008;
a[256][182] = 450089262;
a[256][183] = 551043738;
a[256][184] = 337388940;
a[256][185] = 512108856;
a[256][186] = 28879011;
a[256][187] = 690040638;
a[256][188] = 106017282;
a[256][189] = 558262341;
a[256][190] = 99972432;
a[256][191] = 608226003;
a[256][192] = 612152037;
a[256][193] = 42414435;
a[256][194] = 776201013;
a[256][195] = 39580443;
a[256][196] = 518796945;
a[256][197] = 494437752;
a[256][198] = 583194366;
a[256][199] = 723936555;
a[256][200] = 415359657;
a[256][201] = 309569589;
a[256][202] = 751104774;
a[256][203] = 166684527;
a[256][204] = 249229170;
a[256][205] = 353120823;
a[256][206] = 130668327;
a[256][207] = 753823584;
a[256][208] = 580966092;
a[256][209] = 561963717;
a[256][210] = 543672234;
a[256][211] = 393846327;
a[256][212] = 586278000;
a[256][213] = 327398400;
a[256][214] = 278403867;
a[256][215] = 156455586;
a[256][216] = 363920382;
a[256][217] = 190245195;
a[256][218] = 290039148;
a[256][219] = 547014447;
a[256][220] = 466218648;
a[256][221] = 146037150;
a[256][222] = 585462906;
a[256][223] = 666008595;
a[256][224] = 691786683;
a[256][225] = 374707494;
a[256][226] = 622498779;
a[256][227] = 231158277;
a[256][228] = 685740951;
a[256][229] = 115612245;
a[256][230] = 681825249;
a[256][231] = 545555745;
a[256][232] = 551718468;
a[256][233] = 277206615;
a[256][234] = 640171035;
a[256][235] = 757727334;
a[256][236] = 195193908;
a[256][237] = 658656684;
a[256][238] = 457760646;
a[256][239] = 225925875;
a[256][240] = 505761984;
a[256][241] = 18685233;
a[256][242] = 506832921;
a[256][243] = 112511021;
a[256][244] = 396846646;
a[256][245] = 290147622;
a[256][246] = 113924623;
a[256][247] = 669986155;
a[256][248] = 336008070;
a[256][249] = 63611061;
a[256][250] = 238586775;
a[256][251] = 119956662;
a[256][252] = 616557739;
a[256][253] = 772784623;
a[256][254] = 334527774;
a[256][255] = 410403148;
a[256][256] = 51933421;
cout << a[n][k] << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int zc = 0,oc = 0;
for(int i=0;i<s.size();i++)
{
if(s[i] == '0')zc++;
if(s[i] == '1')oc++;
}
int k = min(zc,oc);
cout<<k*2<<endl;
} | 0 |
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
int n,m;
while ( cin >> n >> m, n || m ) {
double f[101][101];
double dp[101][101];
for ( int i = 0; i < n; i++ ) {
for ( int j = 0; j < n; j++ ) {
cin >> f[i][j];
}
}
for ( int i = 0; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
dp[i][j] = 0.0;
}
}
for ( int i = 0; i < n; i++ ) dp[0][i] = 1.0;
for ( int i = 1; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
for ( int k = 0; k < n; k++ ) {
dp[i][j] = max(dp[i][j], dp[i-1][k] * f[k][j]);
}
}
}
double ans = 0;
for ( int i = 0; i < n; i++ ) ans = max( ans, dp[m-1][i] );
printf("%.2f\n",ans);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void init() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
init();
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
vector<long long int> a_min(n), b_max(n);
for (long long int i = 0; i < n; i++) {
if (i == 0) {
a_min[0] = v[1] - v[0];
b_max[0] = v[n - 1] - v[0];
} else if (i == n - 1) {
a_min[n - 1] = v[n - 1] - v[n - 2];
b_max[i] = v[n - 1] - v[0];
} else {
long long int small = min(v[i + 1] - v[i], v[i] - v[i - 1]);
long long int big = max(v[n - 1] - v[i], v[i] - v[0]);
a_min[i] = small, b_max[i] = big;
}
}
for (long long int i = 0; i < n; i++) {
cout << a_min[i] << " " << b_max[i] << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long n, u, v, w, ans, cnt;
bool mark[N];
vector<int> adj[N];
bool isLucky(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
void dfs(int v) {
mark[v] = 1;
cnt++;
for (auto u : adj[v])
if (!mark[u]) dfs(u);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v >> w;
if (!isLucky(w)) {
adj[u].push_back(v);
adj[v].push_back(u);
}
}
ans = n * (n - 1) * (n - 2);
for (int i = 1; i <= n; i++) {
if (!mark[i]) {
cnt = 0;
dfs(i);
ans -= cnt * (cnt - 1) * (cnt - 2);
ans -= 2 * cnt * (cnt - 1) * (n - cnt);
}
}
cout << ans;
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int sx,sy,tx,ty;
cin>>sx>>sy>>tx>>ty;
string s="";
for(int i=0;i<ty-sy;i++) s+="U";
for(int i=0;i<tx-sx;i++) s+="R";
for(int i=0;i<ty-sy;i++) s+="D";
for(int i=0;i<=tx-sx;i++) s+="L";
for(int i=0;i<=ty-sy;i++) s+="U";
for(int i=0;i<=tx-sx;i++) s+="R";
s+="DR";
for(int i=0;i<=ty-sy;i++) s+="D";
for(int i=0;i<=tx-sx;i++) s+="L";
s+="U";
cout<<s<<endl;
} | 0 |
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
#define MAXN 100000000
int h,w,k,x,y;
long long memory[MAXN], ans[12];
int Sep,tot;
long long z;
int main() {
scanf("%d%d%d",&h,&w,&k);
z=1ll*(h-2)*(w-2);
for(int p=1;p<=k;p++) {
scanf("%d%d",&x,&y);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(1<=x-i&&x-i<=h-2 && 1<=y-j&&y-j<=w-2)
memory[++tot]=MAXN*(x-i)+(y-j);
}
sort(memory+1,memory+1+tot);
int cnt=1;
for(int i=1;i<=tot;i++) {
if(memory[i]==memory[i+1])
cnt++;
else
ans[cnt]++, cnt=1, z--;
}
printf("%lld\n",z);
for(int i=1;i<=9;i++)
printf("%lld\n",ans[i]);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, n, b, c = 0, d = 0;
cin >> n;
cout << n << " ";
for (int i = n / 2; i > 0; i--) {
if (n % i == 0) {
cout << i << " ";
n = i;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = pow(2,31)-1;
const int MAX_N = 1<<17;
int n_;
int dat[2*MAX_N-1];
void init(int n) {
n_ = 1;
while (n_<n) {
n_ *= 2;
}
for (int i=0; i<2*n_-1; i++) {
dat[i] = INF;
}
}
void update(int i,int x) {
i += n_-1;
dat[i] = x;
while (i>0) {
i = (i-1)/2;
dat[i] = min(dat[i*2+1],dat[i*2+2]);
}
}
int find(int x,int y,int k,int l,int r) {
if (r<=x || y<=l) {
return INF;
}
if (x<=l && r<=y) {
return dat[k];
} else {
int vl = find(x,y,k*2+1,l,(l+r)/2);
int vr = find(x,y,k*2+2,(l+r)/2,r);
return min(vl,vr);
}
}
int main() {
int n, q;
cin >> n >> q;
init(n);
for (int i=0; i<q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com==0) {
update(x,y);
} else {
cout << find(x,y+1,0,0,n_) << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d, e, f, sum1, sum2, max;
cin >> a >> b >> c >> d;
if (a % b == 0)
cout << "0" << endl;
else {
sum1 = min((b - (a % b)) * c, (a % b) * d);
cout << sum1 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int latin[27] = {0};
char next(int k) {
for (int i = k - 1; i >= 0; --i) {
if (latin[i] == 0) return i + 'a';
}
return 'a';
}
int main() {
int k;
cin >> k;
string s;
cin >> s;
int l = s.length();
if (l < k) {
cout << "IMPOSSIBLE" << endl;
return 0;
}
for (int i = 0; i < l; ++i) {
if (isalpha(s[i])) latin[s[i] - 'a'] = 1;
}
bool b = false;
int left = l / 2 - 1, right = l / 2;
if (l & 1) {
if (s[l / 2] == '?') {
char aux = next(k);
s[l / 2] = aux;
latin[aux - 'a'] = 1;
}
right++;
}
for (int i = left, j = right; i >= 0 && j < l; --i, ++j) {
if (s[i] != '?' && s[j] == '?')
s[j] = s[i];
else if (s[j] != '?' && s[i] == '?')
s[i] = s[j];
else if (isalpha(s[i]) && isalpha(s[j])) {
if (s[i] != s[j]) {
b = true;
break;
}
} else {
char c = next(k);
if (c == '-') {
s[i] = s[j] = 'a';
} else {
s[i] = s[j] = c;
latin[c - 'a'] = 1;
}
}
}
for (int i = 0; i < k; ++i)
if (latin[i] == 0) {
b = true;
break;
}
if (b)
cout << "IMPOSSIBLE" << endl;
else
cout << s << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct pt {
long long x, y;
} arr[100000];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long o = 0;
for (long long i = 0; i <= n; i++) {
cin >> arr[i].x >> arr[i].y;
if (i == 0) continue;
o += (arr[i].x - arr[i - 1].x) * (arr[i].y + arr[i - 1].y);
}
long long ans = 0;
char curr = 'N';
if (o > 0) {
for (long long i = 1; i < n; i++) {
if (curr == 'W') {
if ((arr[i + 1].x == arr[i].x) && (arr[i + 1].y > arr[i].y)) {
curr = 'N';
continue;
}
ans++;
{
curr = 'S';
continue;
}
}
if (curr == 'E') {
if ((arr[i + 1].x == arr[i].x) && (arr[i + 1].y < arr[i].y)) {
curr = 'S';
continue;
}
ans++;
{
curr = 'N';
continue;
}
}
if (curr == 'N') {
if ((arr[i + 1].y == arr[i].y) && (arr[i + 1].x > arr[i].x)) {
curr = 'E';
continue;
}
ans++;
{
curr = 'W';
continue;
}
}
if (curr == 'S') {
if ((arr[i + 1].y == arr[i].y) && (arr[i + 1].x < arr[i].x)) {
curr = 'W';
continue;
}
ans++;
{
curr = 'E';
continue;
}
}
}
} else {
for (long long i = 1; i < n; i++) {
if (curr == 'W') {
if ((arr[i + 1].x == arr[i].x) && (arr[i + 1].y < arr[i].y)) {
curr = 'S';
continue;
}
ans++;
{
curr = 'N';
continue;
}
}
if (curr == 'E') {
if ((arr[i + 1].x == arr[i].x) && (arr[i + 1].y > arr[i].y)) {
curr = 'N';
continue;
}
ans++;
{
curr = 'S';
continue;
}
}
if (curr == 'N') {
if ((arr[i + 1].y == arr[i].y) && (arr[i + 1].x < arr[i].x)) {
curr = 'W';
continue;
}
ans++;
{
curr = 'E';
continue;
}
}
if (curr == 'S') {
if ((arr[i + 1].y == arr[i].y) && (arr[i + 1].x > arr[i].x)) {
curr = 'E';
continue;
}
ans++;
{
curr = 'W';
continue;
}
}
}
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[1000005], m;
int main() {
cin >> n;
for (int i = (1); i <= (n); i++) {
scanf("%d", &a[i]);
}
cin >> m;
int maxx = 0;
long long maxh = a[1];
for (int i = (1); i <= (m); i++) {
int w, h;
scanf("%d%d", &w, &h);
for (int j = (maxx + 1); j <= (w); j++) {
if (a[j] > maxh) {
maxh = a[j];
}
}
printf("%I64d\n", maxh);
maxh = maxh + h;
maxx = max(maxx, w);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a = 0, b = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (x == 1)
a++;
else
b++;
}
long long ans = 0;
long long x = 1, y = 2;
if (a == 1 || a == 0) ans = 1;
if (a == 2) ans = 2;
for (int i = 3; i <= a; i++) {
ans = ((i - 1) * x + y) % 1000000007;
x = y;
y = ans;
}
for (int i = a + 1; i <= a + b; i++) ans = (ans * i) % 1000000007;
printf("%I64d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, ta;
cin >> a >> ta;
int b, tb, h = 0, m = 0, num;
int totmin;
cin >> b >> tb;
string s;
cin >> s;
for (int i = 0; i < 2; i++) {
h = h * 10 + (s[i] - '0');
}
for (int i = 3; i < 5; i++) {
m = m * 10 + (s[i] - '0');
}
totmin = h * 60 + m;
int counter = 0;
for (int i = 300; i <= 1439; i += b) {
if ((i < totmin && i + tb > totmin) || (i >= totmin && i < totmin + ta))
counter++;
}
cout << counter;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Square(ll x) { return x * x; }
ll Divide(ll x, ll d) {
d = min(d, x);
ll group1 = d - x % d;
ll group2 = x % d;
ll res = group1 * Square(x / d) + group2 * Square(x / d + 1);
return res;
}
int main() {
ll i, j, k;
ll n;
cin >> n >> k;
priority_queue<pair<ll, pair<ll, ll>>> pq;
ll res = 0;
for (i = 0; i < n; ++i) {
cin >> j;
res += Divide(j, 1);
pq.push({Divide(j, 1) - Divide(j, 2), {j, 1}});
}
for (i = n; i < k; ++i) {
pair<ll, ll> p = pq.top().second;
res -= Divide(p.first, p.second);
res += Divide(p.first, p.second + 1);
pq.pop();
pq.push({Divide(p.first, p.second + 1) - Divide(p.first, p.second + 2),
{p.first, p.second + 1}});
}
cout << res << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0;
cin >> n;
int arr[n];
vector<int> vec;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 1) c++;
if (i && arr[i] == 1) vec.push_back(arr[i - 1]);
}
vec.push_back(arr[n - 1]);
cout << c << endl;
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const double PI = acos(-1);
using namespace std;
const int MAX = 100 * 1000 + 10;
vector<int> E[MAX];
int fr[MAX], to[MAX], weight[MAX];
int siz[MAX], max_subtree[MAX], dont[MAX];
int dfs_in[MAX], dfs_out[MAX], dfs_time;
int n, max_l, max_w;
long long ans = 0;
vector<int> vertices;
int rev[MAX], path_len[MAX];
long long path_weight[MAX];
vector<int> ST[MAX << 2][2];
int IDX[MAX][20];
int dfs_cnt(int u, int p) {
siz[u] = 1;
max_subtree[u] = 0;
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
int t = dfs_cnt(v, u);
siz[u] += t;
max_subtree[u] = max(max_subtree[u], t);
}
return siz[u];
}
void get_root(int u, int p, int V, int& root) {
max_subtree[u] = max(max_subtree[u], V - siz[u]);
if (root == -1 || max_subtree[u] < max_subtree[root]) root = u;
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
get_root(v, u, V, root);
}
}
void merge(vector<int>& A, vector<int>& B, vector<int>& C) {
int i = 0, j = 0;
while (i < ((int)A.size()) && j < ((int)B.size())) {
if (path_len[A[i]] < path_len[B[j]])
C.push_back(A[i++]);
else
C.push_back(B[j++]);
}
while (i < ((int)A.size())) C.push_back(A[i++]);
while (j < ((int)B.size())) C.push_back(B[j++]);
}
void build(int node, int s, int e, int depth) {
for (int i = 0; i < (2); i++) ST[node][i].clear();
ST[node][1].resize(e - s + 1);
if (s == e) {
int u = vertices[s];
ST[node][0].push_back(u);
IDX[u][depth] = 0;
return;
}
int m = (s + e) >> 1, left = node << 1, right = left | 1;
build(left, s, m, depth + 1);
build(right, m + 1, e, depth + 1);
merge(ST[left][0], ST[right][0], ST[node][0]);
for (int i = 0; i < (e - s + 1); i++) {
int u = ST[node][0][i];
IDX[u][depth] = i;
}
}
inline void update(int p, int v, vector<int>& BIT) {
for (++p; p <= ((int)BIT.size()); p += ((p) & (-(p)))) BIT[p - 1] += v;
}
inline int get(int p, vector<int>& BIT) {
int ret = 0;
for (++p; p; p ^= ((p) & (-(p)))) ret += BIT[p - 1];
return ret;
}
void update(int node, int s, int e, int pos, int depth) {
update(IDX[vertices[pos]][depth], 1, ST[node][1]);
if (s == e) return;
int m = (s + e) >> 1, left = node << 1, right = left | 1;
if (pos <= m)
update(left, s, m, pos, depth + 1);
else
update(right, m + 1, e, pos, depth + 1);
}
int query(int node, int s, int e, int pos, int len) {
if (e <= pos) {
int p = lower_bound(ST[node][0].begin(), ST[node][0].end(), len + 1,
[](const int& a, const int& target) {
return path_len[a] < target;
}) -
ST[node][0].begin();
return get(p - 1, ST[node][1]);
}
int m = (s + e) >> 1, left = node << 1, right = left | 1;
if (pos <= m)
return query(left, s, m, pos, len);
else
return query(left, s, m, pos, len) + query(right, m + 1, e, pos, len);
;
}
void explore(int u, int p, int len, long long w) {
vertices.push_back(u);
path_len[u] = len;
path_weight[u] = w;
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
explore(v, u, len + 1, w + weight[e]);
}
}
void compute(int u, int p) {
auto idx =
lower_bound(vertices.begin(), vertices.end(), max_w - path_weight[u] + 1,
[](const int& a, const long long& target) {
return path_weight[a] < target;
}) -
vertices.begin();
if (idx) {
--idx;
int tmp = query(1, 0, ((int)vertices.size()) - 1, idx, max_l - path_len[u]);
ans += tmp;
}
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
compute(v, u);
}
}
void insert(int u, int p) {
update(1, 0, ((int)vertices.size()) - 1, rev[u], 0);
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
insert(v, u);
}
}
void divide_conquer(int u, int p) {
dont[p] = 1;
int V = dfs_cnt(u, p), root = -1;
if (V == 1) return;
get_root(u, p, V, root);
assert(root != -1);
u = root;
vertices.clear();
vertices.push_back(u);
path_weight[u] = path_len[u] = 0;
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
explore(v, u, 1, weight[e]);
}
sort(vertices.begin(), vertices.end(), [](const int& a, const int& b) {
return tie(path_weight[a], path_len[a]) < tie(path_weight[b], path_len[b]);
});
for (int i = 0; i < (((int)vertices.size())); i++) rev[vertices[i]] = i;
build(1, 0, ((int)vertices.size()) - 1, 0);
update(1, 0, ((int)vertices.size()) - 1, rev[u], 0);
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
compute(v, u);
insert(v, u);
}
for (int e : E[u]) {
int v = fr[e] + to[e] - u;
if (v == p || dont[v]) continue;
divide_conquer(v, u);
}
}
int main() {
scanf("%d %d %d", &n, &max_l, &max_w);
for (int e = 0; e < (n - 1); e++) {
to[e] = e + 2;
scanf("%d %d", fr + e, weight + e);
E[fr[e]].push_back(e);
E[to[e]].push_back(e);
}
vertices.reserve(n);
divide_conquer(1, 0);
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long k1, k2, n, m, use[200000], use1[200000];
vector<pair<int, int> > edge[200000];
vector<int> dp[2][2], aa, bb;
int dfs(int i, int j) {
int sum = 0;
if (use[i]) aa.push_back(i);
for (auto v : edge[i]) {
int k = (use[i] + v.second) % 2, p = 0;
if (k != j) p = 1;
if (use[v.first] != -1) {
if (use[v.first] != p) {
return n + 1;
}
} else {
use[v.first] = p;
sum += p + dfs(v.first, j);
}
}
return sum;
}
int dfs1(int i, int j) {
int sum = 0;
if (use1[i]) bb.push_back(i);
for (auto v : edge[i]) {
int k = (use1[i] + v.second) % 2, p = 0;
if (k != j) p = 1;
if (use1[v.first] != -1) {
if (use1[v.first] != p) return n + 1;
} else {
use1[v.first] = p;
sum += p + dfs1(v.first, j);
}
}
return sum;
}
signed main() {
iostream::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < int(m); i++) {
int a, b;
char c;
cin >> a >> b >> c;
a--;
b--;
int k = 0;
if (c == 'R') k = 1;
edge[a].push_back({b, k});
edge[b].push_back({a, k});
}
long long ans = n + 1;
int nu;
for (int j = 0; j <= 1; j++) {
for (int i = 0; i < int(n); i++) {
use[i] = -1;
use1[i] = -1;
}
long long k = 0;
for (int i = 0; i < int(n); i++) {
if (use[i] == -1) {
use[i] = 0;
aa.resize(0);
bb.resize(0);
if (dfs(i, j) > n) aa.resize(n + 1);
use1[i] = 1;
if (dfs1(i, j) > n) bb.resize(n + 1);
k += min((int)aa.size(), (int)bb.size());
}
}
ans = min(ans, k);
if (ans == k) {
nu = j;
}
}
if (ans == n + 1) {
cout << -1;
return 0;
}
cout << ans << "\n";
for (int i = 0; i < int(n); i++) {
use[i] = -1;
use1[i] = -1;
}
int j = nu;
vector<long long> ans1;
for (int i = 0; i < int(n); i++) {
if (use[i] == -1) {
use[i] = 0;
aa.resize(0);
bb.resize(0);
if (dfs(i, j) > n) aa.resize(n + 1);
use1[i] = 1;
if (dfs1(i, j) > n) bb.resize(n + 1);
if ((int)aa.size() < (int)bb.size()) {
for (int i = 0; i < int((int)aa.size()); i++) ans1.push_back(aa[i] + 1);
} else
for (int i = 0; i < int((int)bb.size()); i++) ans1.push_back(bb[i] + 1);
}
}
sort(ans1.begin(), ans1.end());
for (int i = 0; i < int((int)ans1.size()); i++) cout << ans1[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, kol = 0;
cin >> n;
long long a[11] = {0};
long long b[11] = {0};
long long k;
while (cin >> k) {
k >= 0 ? a[k]++ : b[-1 * k]++;
}
kol += (a[0] - 1) * a[0] / 2;
for (int i = 1; i < 11; i++) {
kol += (a[i] * b[i]);
}
cout << kol;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long dp[(1 << 20)];
int msk;
int cnt[21][30];
long long c[21][30];
long long mxc[21][30];
int chMsk[21][30];
long long g[21][21];
int sm[(1 << 20)];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<string> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) cin >> g[i][j];
for (int i = (1 << N) - 1; i >= 0; i--) {
for (int j = 0; j < N; j++) {
if (!(i & (1 << j))) {
sm[i] = j;
break;
}
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cnt[i][a[j][i] - 'a']++;
chMsk[i][a[j][i] - 'a'] |= (1 << j);
c[i][a[j][i] - 'a'] += g[j][i];
mxc[i][a[j][i] - 'a'] = max(mxc[i][a[j][i] - 'a'], g[j][i]);
}
}
dp[(1 << N) - 1] = 0;
for (int i = (1 << N) - 2; i >= 0; i--) {
dp[i] = INT_MAX;
int k = sm[i];
for (int j = 0; j < M; j++) {
dp[i] = min(dp[i], g[k][j] + dp[i | (1 << k)]);
dp[i] = min(dp[i], c[j][a[k][j] - 'a'] - mxc[j][a[k][j] - 'a'] +
dp[i | chMsk[j][a[k][j] - 'a']]);
}
}
cout << dp[0] << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
int res = 1;
while (res * (res - 1) / 2 <= n) res++;
res--;
vector<int> Q[res + 10];
int cnt = 1;
for (int i = 1; i <= res; i++) {
for (int j = i + 1; j <= res; j++) {
Q[i].push_back(cnt);
Q[j].push_back(cnt);
cnt++;
}
}
cout << res << endl;
for (int i = 1; i <= res; i++) {
for (int j = 0; j < (int)Q[i].size(); j++) cout << Q[i][j] << ' ';
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000007;
long long int cnt[200005];
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long int n, a = 0, b = 0, k1, k2, p, c, x, mx = 0, mn = 0, i, j,
flag = 0, cnt = 0;
cin >> n;
string s, s1, s2;
cin >> s;
for (i = 0; i < n - 1; i += 2) {
if (s[i] == 'a' && s[i + 1] == 'a') {
s[i + 1] = 'b';
cnt++;
} else if (s[i] == 'b' && s[i + 1] == 'b') {
s[i + 1] = 'a';
cnt++;
}
}
cout << cnt << endl;
cout << s << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
while (a <= ans) a += b;
ans = a;
}
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T x, T y) {
while (T t = x % y) x = y, y = t;
return y;
}
const double eps = 1e-10;
const double PI = acos(-1.);
const int INF = 1000000000;
const int MOD = 1000000007;
const double E = 2.7182818284590452353602874713527;
bool isdig(char x) { return x >= '0' && x <= '9'; }
bool isup(char x) { return x >= 'A' && x <= 'Z'; }
bool islow(char x) { return x >= 'a' && x <= 'z'; }
bool islet(char x) { return isup(x) || islow(x); }
vector<int> vec;
string s;
void get_data() {
vec.clear();
s = "";
string t;
while (cin >> t && t[0] != '#') s += t;
}
int stk[100005], sn;
void run() {
sn = 0;
for (int i = 0; i < ((int)(s).size()); i++) {
if (s[i] == 'a') {
if (s[i - 2] == '/') {
vec.push_back(stk[sn - 1]);
sn--;
} else {
stk[sn++] = 0;
}
} else if (s[i] == 'd')
stk[sn - 1]++;
}
sort((vec).begin(), (vec).end());
for (int i = 0; i < ((int)(vec).size()); i++) cout << vec[i] / 2 << ' ';
cout << endl;
}
int main(int argc, char *argv[]) {
{
get_data();
run();
}
return EXIT_SUCCESS;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 8, i, c = 0, d = 0;
string s;
while (n--) {
cin >> s;
for (i = 0; i < s.length(); i++) {
if (s[i] == 'Q') c = c + 9;
if (s[i] == 'R') c = c + 5;
if (s[i] == 'B') c = c + 3;
if (s[i] == 'N') c = c + 3;
if (s[i] == 'P') c = c + 1;
if (s[i] == 'K') c = c + 0;
if (s[i] == 'q') d = d + 9;
if (s[i] == 'r') d = d + 5;
if (s[i] == 'b') d = d + 3;
if (s[i] == 'n') d = d + 3;
if (s[i] == 'p') d = d + 1;
if (s[i] == 'k') d = d + 0;
}
}
if (c > d) cout << "White";
if (d > c) cout << "Black";
if (c == d) cout << "Draw";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[1005];
bool vis[1005], isgov[1005], hasgov;
int gov[1005], fvc, fec, vcnt, ecnt;
vector<pair<int, int> > govres;
void dfs(int v) {
if (isgov[v]) hasgov = true;
vis[v] = true;
vcnt++, ecnt += G[v].size();
for (int u : G[v])
if (!vis[u]) dfs(u);
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; i++) {
scanf("%d", gov + i);
isgov[gov[i] - 1] = true;
}
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
G[--x].push_back(--y);
G[y].push_back(x);
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
hasgov = false;
vcnt = ecnt = 0;
dfs(i);
ecnt >>= 1;
if (!hasgov)
fvc += vcnt, fec += ecnt;
else
govres.emplace_back(vcnt, ecnt);
}
int edges = 0;
int maxv = 0;
for (auto [vcnt, ecnt] : govres) {
edges += vcnt * (vcnt - 1) / 2 - ecnt;
if (vcnt > maxv) maxv = vcnt;
}
int res = maxv * fvc + fvc * (fvc - 1) / 2 - fec + edges;
printf("%d\n", res);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
map<int, int> mm;
int main() {
cin >> n >> m;
int arr[n], ar[m];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
cin >> ar[i];
mm[ar[i]]++;
}
for (int i = 0; i < n; i++)
if (mm[arr[i]] > 0) cout << arr[i] << " ";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn];
int n;
int cnt[maxn];
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(cnt, 0, sizeof(cnt));
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
int l = 1;
int r = 2;
int ans = 1e9;
cnt[a[l]] = 1;
while (1) {
while (r <= n) {
if (!cnt[a[r]])
cnt[a[r]] = 1;
else {
cnt[a[r]]++;
break;
}
r++;
}
if (r > n) break;
while (cnt[a[l]] != 2) {
cnt[a[l]]--, l++;
}
cnt[a[l]]--;
l++;
r++;
ans = min(ans, r - l + 1);
}
if (ans != 1e9)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int T = 20;
const int INF = 1e9;
int n, k;
int a[N];
int step[N];
pair<int, int> g[T][N];
vector<int> add[N];
vector<int> sub[N];
int st[N];
long long sum[N];
int cnt[N];
pair<int, int> getMax(int l, int r) {
int len = r - l;
return max(g[st[len]][l], g[st[len]][r - (1 << st[len])]);
}
long long getSum(int l, int r) { return sum[r] - sum[l]; }
void calc(int l, int r) {
if (l >= r) return;
auto res = getMax(l, r);
int x = res.second;
int len = r - l;
if ((x - l) * 2 < len) {
for (int i = l; i <= x; i++) {
long long s = (k - getSum(i, x) % k + sum[x + 1]) % k;
add[r + 1].push_back(s);
sub[x + 1].push_back(s);
}
} else {
for (int i = x; i < r; i++) {
long long s = (getSum(x + 1, i + 1) + sum[x]) % k;
add[x + 1].push_back(s);
sub[l].push_back(s);
}
}
calc(l, x);
calc(x + 1, r);
}
void read() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
}
void solve() {
for (int i = 0; i < n; i++) g[0][i] = make_pair(a[i], i);
for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + a[i];
for (int j = 1; j < T; j++)
for (int i = 0; i + (1 << j) <= n; i++)
g[j][i] = max(g[j - 1][i], g[j - 1][i + (1 << (j - 1))]);
for (int i = 2; i <= n; i++) st[i] = st[i / 2] + 1;
calc(0, n);
long long answer = 0;
for (int i = 0; i <= n + 1; i++) {
for (auto x : sub[i]) answer -= cnt[x];
for (auto x : add[i]) answer += cnt[x];
cnt[sum[i] % k]++;
}
cout << answer - n << endl;
}
void printAns() {}
void stress() {}
int main() {
if (1) {
int k = 1;
for (int tt = 0; tt < k; tt++) {
read();
solve();
printAns();
}
} else {
stress();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, p[N][N], vis[N];
long double E[N], prob[N], coef[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) E[i] = 1e30, prob[i] = 1, coef[i] = 0;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
long double mv = 1e30;
int ps = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && E[i] < mv) mv = E[i], ps = i;
vis[ps] = 1;
if (ps == 0) {
printf("%.15f\n", (double)E[0]);
return 0;
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
coef[i] += E[ps] * prob[i] * 0.01 * p[i][ps];
prob[i] *= (1 - 0.01 * p[i][ps]);
if (prob[i] < 1 - 1e-6) E[i] = (1 + coef[i]) / (1 - prob[i]);
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[3005];
bool b[3005];
int main() {
int n, m, p1v = 0;
cin >> n >> m;
vector<pair<long long int, int> > v;
vector<pair<long long int, int> > x;
for (int i = 2; i <= m; i++) x.push_back(make_pair(0, i));
for (int i = 0; i < n; i++) {
long long int p, c;
cin >> p >> c;
if (p == 1) {
p1v++;
} else {
v.push_back(make_pair(c, p));
x[p - 2].first++;
}
}
sort((x).begin(), (x).end());
reverse((x).begin(), (x).end());
sort((v).begin(), (v).end());
long long int cost = 0, ans = LLONG_MAX;
for (int vt = p1v; vt < 3001; vt++) {
cost = 0;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (auto p : x)
if (p.first >= vt) a[p.second] = p.first - vt + 1;
int req = vt - p1v;
for (int i = 0; i < (int)v.size(); i++) {
auto p = v[i];
if (a[p.second] > 0) {
a[p.second]--;
b[i] = 1;
req--;
}
}
if (req < 0) {
continue;
}
for (int i = 0; i < (int)v.size(); i++) {
if (b[i])
cost += v[i].first;
else {
if (req > 0) {
cost += v[i].first;
req--;
}
}
}
if (req == 0) ans = min(ans, cost);
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > Q;
long long res;
int n, A[200010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
for (int i = 1; i <= n; i++) Q.push(A[i]);
res = 0;
if (!(n % 2)) {
long long t1 = Q.top();
Q.pop();
long long t2 = Q.top();
Q.pop();
res += (t1 + t2);
Q.push(t1 + t2);
}
while (Q.size() > 1) {
long long t1 = Q.top();
Q.pop();
long long t2 = Q.top();
Q.pop();
long long t3 = Q.top();
Q.pop();
res += (t1 + t2 + t3);
Q.push(t1 + t2 + t3);
}
printf("%lld", res);
return 0;
}
| 4 |
#include <iostream>
#include <vector>
using namespace std;
long long const mod = 1000000007;
long long mod_pow(long long a, long long b){
long long res = 1;
a %= mod;
while(b > 0){
if(b%2 == 1) res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
long long mod_div(long long a){
return mod_pow(a, mod-2);
}
int main(void){
long long n, a, b, c, ab, an, ak, bn, bk, kcn, ans = 0;
cin >> n >> a >> b >> c;
ab = mod_div(a+b);
a = a*ab % mod;
b = b*ab % mod;
an = mod_pow(a, n);
bn = mod_pow(b, n);
ak = bk = kcn = 1;
for(long long k = n; k < n*2; k++){
ans += (an * bk + bn * ak) % mod * kcn % mod * k % mod;
ak = ak*a % mod;
bk = bk*b % mod;
kcn = kcn*k % mod * mod_div(k-n+1) % mod;
}
ans = ans * 100 % mod * mod_div(100-c) % mod;
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, a[200005];
cin >> n;
for (int i = 1; i <= 2 * n; i++) cin >> a[i];
sort(a + 1, a + 2 * n + 1);
cout << a[n + 1] - a[n] << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int oo = 2e9;
const long long loo = 2e18;
using namespace std;
int main() {
int n;
cin >> n;
long long a[N];
for (int i = 0; i < n; ++i) cin >> a[i];
int m;
cin >> m;
long long h = a[0], x, y;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
cout << max(h, a[x - 1]) << endl;
h = max(h, a[x - 1]) + y;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int a, b, i, j;
scanf("%d%d", &a, &b);
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (i % 2 == 0)
printf("#");
else if (j < b - 1 && i % 4 == 1)
printf(".");
else if (i % 4 == 3 && j > 0)
printf(".");
else if (i % 4 == 1 && j == b - 1)
printf("#");
else if (i % 4 == 3 && j == 0)
printf("#");
}
printf("\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long qp(long long a, long long b, long long mod) {
long long t = 1;
while (b) {
if (b & 1) t = t * a % mod;
b >>= 1;
a = a * a % mod;
}
return t;
}
struct DOT {
int x;
int y;
};
inline void read(int &x) {
int k = 0;
char f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) k = k * 10 + c - '0';
x = k * f;
}
void ex() {
puts("No");
exit(0);
}
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
const int inf = 0x3f3f3f3f;
const long long Linf = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
;
const int maxn = 1e5 + 34;
int n, a[maxn];
map<int, int> cnt;
map<int, int> lb, rb;
int main() {
scanf("%d", &n);
int mx = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
mx = max(mx, cnt[a[i]]);
if (cnt[a[i]] == 1) {
lb[a[i]] = i;
}
rb[a[i]] = i;
}
int ans = n;
int ansl = 1;
map<int, int>::iterator it;
for (it = cnt.begin(); it != cnt.end(); it++) {
if (it->second == mx) {
int tmp = rb[it->first] - lb[it->first] + 1;
if (ans > tmp) {
ans = tmp;
ansl = lb[it->first];
}
}
}
printf("%d %d\n", ansl, ansl + ans - 1);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<string> vec;
int cmp(string a, string b) {
int i = 0, j = 0, cnt = 0;
for (; i < a.size(); i++) {
if (a[i] == b[j])
j++;
else
cnt++;
}
return (b.size() - j) + cnt;
}
void solve() {
string s;
cin >> s;
int mins = 1e6;
for (int i = 0; i < 64; i++) {
mins = min(mins, cmp(s, vec[i]));
}
cout << mins << endl;
}
int main() {
for (int i = 0; i <= 63; i++) vec.push_back(to_string(1ll << i));
int T;
cin >> T;
while (T--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, M = 3e5 + 7;
int powe(int x, int y) {
x = x % mod;
int ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
vector<vector<int> > edg(M);
int arr[M];
int pas[M];
set<int> pass, lis;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, m, ctr = 0, a, b;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
edg[a].push_back(b);
if (b == arr[n - 1]) pas[a] = 1;
}
for (int i = n - 2; i >= 0; i--) {
if (pas[arr[i]]) {
bool pass = false;
if (edg[arr[i]].size() < lis.size()) {
lis.insert(arr[i]);
continue;
}
sort(edg[arr[i]].begin(), edg[arr[i]].end());
auto i2 = lis.begin();
int i1 = 0, lim = edg[arr[i]].size();
for (; i2 != lis.end() && i1 < lim;) {
while (i1 < n && edg[arr[i]][i1] < *i2) {
i1++;
}
if (edg[arr[i]][i1] == *i2) {
i1++, i2++;
} else
break;
}
if (i2 == lis.end()) {
pass = true;
}
if (pass)
ctr++;
else {
lis.insert(arr[i]);
}
} else
lis.insert(arr[i]);
}
cout << ctr;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10010];
a[0]=1;
a[1]=2;
for(int i=2;i<10010;i++){
a[i]=a[i-1]+i;
}
int n;
while(cin>>n){
cout<<a[n]<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long ans1, ans2, ans3;
void solve(long long x, long long y) {
if (y == 0) {
ans3 = x;
ans1 = 1;
ans2 = 0;
return;
}
solve(y, x % y);
long long x2 = ans1;
ans1 = ans2;
ans2 = x2 - (x / y) * ans2;
}
long long ceil(long long x, long long y) {
if (x % y == 0) {
return x / y;
}
return x / y + 1;
}
signed main() {
long long n, i, j, k, l, a, b, x, y, m, now = 0;
cin >> n >> m >> x >> y >> a >> b;
if (a == 0) {
if (b == -1) {
if (x == n || x == 0) {
cout << x << ' ' << 0;
} else {
cout << -1;
}
} else {
if (x == n || x == 0) {
cout << x << ' ' << m;
} else {
cout << -1;
}
}
return 0;
}
if (b == 0) {
if (a == -1) {
if (y == m || y == 0) {
cout << 0 << ' ' << y;
} else {
cout << -1;
}
} else {
if (y == m || y == 0) {
cout << n << ' ' << y;
} else {
cout << -1;
}
}
return 0;
}
if (a == -1) {
now -= x;
} else {
now -= (n - x);
}
if (b == -1) {
now += y;
} else {
now += (m - y);
}
solve(n, m);
if (now % ans3 != 0) {
cout << -1;
return 0;
}
ans1 *= now / ans3;
ans2 *= now / ans3;
if (ans1 < 0) {
i = ceil(-ans1, m / ans3);
ans1 += (m / ans3) * i;
ans2 -= (n / ans3) * i;
}
i = ans1 / (m / ans3);
ans1 -= (m / ans3) * i;
ans2 += (n / ans3) * i;
ans2 *= -1;
if (a == -1) {
if (ans1 % 2 == 0) {
ans1 = 0;
} else {
ans1 = n;
}
} else {
if (ans1 % 2 == 0) {
ans1 = n;
} else {
ans1 = 0;
}
}
if (b == -1) {
if (ans2 % 2 == 0) {
ans2 = 0;
} else {
ans2 = m;
}
} else {
if (ans2 % 2 == 0) {
ans2 = m;
} else {
ans2 = 0;
}
}
cout << ans1 << ' ' << ans2;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long int n, k;
long double l, v1, v2;
cin >> n >> l >> v1 >> v2 >> k;
long long int g = (n + k - 1) / k;
long double d, lo = 0, hi = 1e9 + 5;
for (int T = 0; T < 72; T++) {
d = (lo + hi) / 2;
long double res = d * (g - (g - 1) * (v2 - v1) / (v2 + v1)) - l;
if (res > 0)
hi = d;
else
lo = d;
}
cout << fixed << setprecision(20)
<< d * (g + (g - 1) * (v2 - v1) / (v2 + v1)) / v2 << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string S;
cin >>S;
cout <<S.substr(0, S.size()-8) <<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "RDLU";
const long long MOD2 = (long long)1000000007 * (long long)1000000007;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int lg[500135];
struct mb {
int T[500135][20];
void init(int n) {
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
T[i][j] = max(T[i][j - 1], T[i + (1 << (j - 1))][j - 1]);
}
}
}
int qym(int l, int r) {
if (r < l) swap(l, r);
int x = lg[r - l + 1];
int y = max(T[l][x], T[r - (1 << x) + 1][x]);
return y;
}
} T[2][2];
int a[500135], ans[500135], ctm[500135];
int qy(int L, int R, bool big) {
if (L <= 0) {
L = L % 2 == 0 ? 2 : 1;
}
if (R > n) {
R = R % 2 == n % 2 ? n : n - 1;
}
assert(L % 2 == R % 2);
int w = L & 1;
if (big)
return T[w][0].qym(L, R);
else
return -T[w][1].qym(L, R);
}
bool ck(int i, int steps, bool sm) {
int l = steps / 2, r = (steps + 1) / 2;
if (sm) {
return qy(i - l * 2, i + l * 2, 1) < qy(i - r * 2 + 1, i + r * 2 - 1, 0);
} else {
return qy(i - l * 2, i + l * 2, 0) > qy(i - r * 2 + 1, i + r * 2 - 1, 1);
}
}
void fmain(int tid) {
scanf("%d", &n);
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", a + i);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
int z = i & 1;
T[z][0].T[i][0] = a[i];
T[z][1].T[i][0] = -a[i];
T[z ^ 1][0].T[i][0] = -(1 << 30);
T[z ^ 1][1].T[i][0] = -(1 << 30);
}
for (int(i) = 0; (i) < (int)(2); (i)++)
for (int(j) = 0; (j) < (int)(2); (j)++) T[i][j].init(n);
int Ans = 0;
for (int(i) = 1; (i) <= (int)(n); (i)++) {
if (i == 1 || i == n) {
ans[i] = a[i];
continue;
}
if (a[i - 1] <= a[i] && a[i] <= a[i + 1]) {
ans[i] = a[i];
continue;
}
if (a[i - 1] >= a[i] && a[i] >= a[i + 1]) {
ans[i] = a[i];
continue;
}
bool sm = a[i - 1] > a[i] && a[i] < a[i + 1];
int pre = ctm[i - 1];
if (ck(i, pre + 1, sm))
pre++;
else if (!ck(i, pre, sm))
pre--;
mintt(pre, min(i - 1, n - i));
ctm[i] = pre;
maxtt(Ans, pre);
ans[i] = qy(i - pre, i + pre, sm ^ (pre & 1));
}
printf("%d\n", Ans);
for (int(i) = 1; (i) <= (int)(n); (i)++) printf("%d ", ans[i]);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int ans[51];
int f, l, n, i;
long long p, m;
cin >> n >> m;
if (n == 1) {
cout << 1 << endl;
return 0;
}
p = 1ll << (n - 2);
f = 0;
l = n - 1;
i = 1;
while (p) {
if (m <= p) {
ans[f++] = i;
} else {
ans[l--] = i;
m -= p;
}
p >>= 1;
i++;
}
ans[f] = i;
printf("%d", ans[0]);
for (i = 1; i < n; i++) {
printf(" %d", ans[i]);
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
map<string, int> indegree;
map<string, string> m, ans;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 1; i <= n; i++) {
string a, b;
cin >> a >> b;
m[a] = b;
indegree[b]++;
}
for (auto it : m) {
string cur = it.first;
if (indegree[cur]) continue;
string original = cur;
while (m[cur].size()) cur = m[cur];
ans[original] = cur;
}
cout << ans.size() << "\n";
for (auto it : ans) {
cout << it.first << " " << it.second << "\n";
}
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.