# AtCoder Beginner Contest 326 题解

### A - 2UP3DOWN

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n,m,a[200005],ans;
signed main(){
IOS;TIE;
cin>>n>>m;
if(m>=n-3&&m<=n+2) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}


### B - 326-like Numbers

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n,m,a[200005],ans;
signed main(){
IOS;TIE;
cin>>n;
for(int i=n;;i++){
if(i/100*(i/10%10)==i%10){
cout<<i<<endl;
return 0;
}
}
return 0;
}


### C - Peak

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n,m,a[300005],ans;
signed main(){
IOS;TIE;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
ans=max(ans,upper_bound(a+i,a+n+1,a[i]+m-1)-a-i);
}
cout<<ans<<endl;
return 0;
}


### D - ABC Puzzle

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
using namespace std;
int n;
string s,t;
char c[35];
int id(int x,int y){
return (x-1)*n+y;
}
bool check(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(c[id(i,j)]!='.'){
if(c[id(i,j)]==s[i]) break;
else return 0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(c[id(j,i)]!='.'){
if(c[id(j,i)]==t[i]) break;
else return 0;
}
}
}

for(int j=1;j<=n;j++){
int fa=0,fb=0,fc=0;
for(int i=1;i<=n;i++){
fa+=(c[id(j,i)]=='A');
fb+=(c[id(j,i)]=='B');
fc+=(c[id(j,i)]=='C');
}
if(fa!=1||fb!=1||fc!=1) return 0;
}
for(int j=1;j<=n;j++){
int fa=0,fb=0,fc=0;
for(int i=1;i<=n;i++){
fa+=(c[id(i,j)]=='A');
fb+=(c[id(i,j)]=='B');
fc+=(c[id(i,j)]=='C');
}
if(fa!=1||fb!=1||fc!=1) return 0;
}
return 1;
}
bool ok(){
for(int j=1;j<=n;j++){
int fa=0,fb=0,fc=0;
for(int i=1;i<=n;i++){
fa+=(c[id(j,i)]=='A');
fb+=(c[id(j,i)]=='B');
fc+=(c[id(j,i)]=='C');
}
if(fa>1||fb>1||fc>1) return 0;
}
for(int j=1;j<=n;j++){
int fa=0,fb=0,fc=0;
for(int i=1;i<=n;i++){
fa+=(c[id(i,j)]=='A');
fb+=(c[id(i,j)]=='B');
fc+=(c[id(i,j)]=='C');
}
if(fa>1||fb>1||fc>1) return 0;
}
return 1;
}
void dfs(int x,int y){
if(!ok()) return ;
if(id(x,+y)==n*n+1){
if(check()){
cout<<"Yes"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cout<<c[id(i,j)];
cout<<endl;
}
exit(0);
}
return ;
}
bool fl1=1,fl2=1;
for(int i=1;i<y;i++) if(c[id(x,i)]!='.') fl1=0;
for(int i=1;i<x;i++) if(c[id(i,y)]!='.') fl2=0;
int yy=y+1,xx=x;
if(yy>n) xx++,yy=1;
dfs(xx,yy);
if(fl1) c[id(x,y)]=s[x],dfs(xx,yy);
else if(fl2) c[id(x,y)]=t[y],dfs(xx,yy);
else{
for(char i='A';i<='C';i++){
c[id(x,y)]=i;
dfs(xx,yy);
}
}
c[id(x,y)]='.';
}
signed main(){
IOS;TIE;
cin>>n>>s>>t;
s=" "+s,t=" "+t;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) c[id(i,j)]='.';
}
dfs(1,1);
cout<<"No"<<endl;
return 0;
}


### E - Revenge of "The Salary of AtCoder Inc."

1 2 3 4

• $$1$$ 不可能被选到
• $$2$$ 产生贡献前提是上一次选到了 $$1$$，然后这次选到 $$2$$。故概率是 $$\frac14\times\frac14=\frac{1}{4^2}$$
• $$3$$ 产生贡献前提是上一次选到了 $$1$$$$2$$，然后这次选到 $$3$$。故概率是 $$(\frac14+\frac14)\times\frac14=\frac{2}{4^2}$$
• $$4$$ 产生贡献前提是上一次选到了 $$1$$$$2$$$$3$$，然后这次选到 $$4$$。故概率是 $$(\frac14+\frac14+\frac14)\times\frac14=\frac{3}{4^2}$$
1 2 3 4

• $$1$$$$2$$ 不可能被选到
• $$3$$ 产生贡献前提是上一次选到了 $$1$$$$2$$，然后这次选到 $$3$$。故概率是 $$(0+\frac{1}{4^2})\times\frac14=\frac{1}{4^3}$$
• $$4$$ 产生贡献前提是上一次选到了 $$1$$$$2$$$$3$$，然后这次选到 $$4$$。故概率是 $$(0+\frac{1}{4^2}+\frac{2}{4^2})\times\frac14=\frac{3}{4^3}$$

1 2 3 4

$\sum _ {i = 1} ^ n \sum _ {j = 1} ^ i \frac{C_{i-1}^{j-1}}{n^j}\times a_i$

$f_i=f_{i-1}+f_{i-1}\times\frac1n$

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 998244353
using namespace std;
int n,a[300005],p,ans,f[300005];
int ksm(int x,int b){
int ans=1;
while(b){
if(b&1) ans=ans*x%mod;
x=x*x%mod,b>>=1;
}
return ans;
}
signed main(){
IOS;TIE;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int INV=ksm(n,mod-2),p=1ll;
f[1]=INV;
ans+=f[1]*a[1]%mod;
for(int i=2;i<=n;i++){
f[i]=f[i-1]+f[i-1]*INV%mod,f[i]%=mod;
ans+=a[i]*f[i]%mod;
ans%=mod;
}
cout<<ans<<endl;
return 0;
}


### G - Unlock Achievement

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define inf 1e15
using namespace std;
int n,m,c[105],a[105],ans,f[55][55];

struct MaxFlow{
struct node{
int to,nxt,f;
}e[50005];
}
queue<int> q;
int bfs(int s,int t){
while(q.size()) q.pop();q.push(s);
memset(dep,0,sizeof(dep));dep[s]=1;
while(q.size()){
int x=q.front();q.pop();
node tmp=e[i];
if(tmp.f&&!dep[tmp.to]){
dep[tmp.to]=dep[x]+1;
q.push(tmp.to);
}
}
}
return dep[t];
}
int dfs(int s,int t,int f){
if(s==t||!f) return f;
int ans=0;
for(int i=cur[s];i;i=e[i].nxt){
node &x=e[i];cur[s]=i;
if(x.f&&dep[x.to]==dep[s]+1){
int tmp=dfs(x.to,t,min(f,x.f));
x.f-=tmp,e[i^1].f+=tmp;
ans+=tmp,f-=tmp;
if(f<=0) break;
}
}
return ans;
}
int dinic(int s,int t){
int ans=0;
while(bfs(s,t)){
ans+=dfs(s,t,inf);
}
return ans;
}
}mf;
signed main(){
IOS;TIE;
cin>>n>>m;
int S=n*5+m+1,T=n*5+m+2;
for(int i=1;i<=n;i++) cin>>c[i];
for(int i=1;i<=m;i++) cin>>a[i],ans+=a[i];
for(int i=1;i<=n;i++){
for(int j=1;j<=4;j++){
}
}
for(int i=1;i<=m;i++){