You can view Chinese editorial here: https://www.luogu.com.cn/blog/Caro23333/codeforces-round-641-zhong-wen-ti-xie

Div2.A Problem and editorial by BlueSmoke

**Editorial**

Tutorial is loading...

**Code**

```
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
int n,k;
cin >> n >> k;
if(n%2==0)
{
cout << n+2*k << endl;
continue;
}
int p = 0;
for(int i = n; i>=2; i--)
if(n%i==0)
p = i;
cout << n+p+2*(k-1) << endl;
}
return 0;
}
```

Div2.B Problem and editorial by BlueSmoke

**Editorial**

Tutorial is loading...

**Code**

```
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 500005;
inline int readint()
{
int res = 0;
char c = 0;
while(!isdigit(c))
c = getchar();
while(isdigit(c))
res = res*10+c-'0', c = getchar();
return res;
}
int n,a[MAXN],f[MAXN];
int main()
{
int T = readint();
while(T--)
{
n = readint();
for(int i = 1; i<=n; i++)
a[i] = readint();
for(int i = 1; i<=n; i++)
f[i] = 1;
for(int i = 1; i<=n; i++)
for(int j = i*2; j<=n; j += i)
if(a[i]<a[j])
f[j] = max(f[j],f[i]+1);
int ans = 0;
for(int i = 1; i<=n; i++)
ans = max(ans,f[i]);
cout << ans << endl;
}
return 0;
}
```

Div1.A Problem and editorial by mydiplomacy

**Editorial**

Tutorial is loading...

**Code**

```
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=100005;
int n;
ll a[maxn];
ll pre[maxn],suf[maxn];
ll gcd(ll x, ll y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
ll ga,ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
pre[1]=a[1]; suf[n]=a[n];
for(int i=2;i<=n;i++)
pre[i]=gcd(pre[i-1],a[i]);
for(int i=n-1;i>=1;i--)
suf[i]=gcd(suf[i+1],a[i]);
for(int i=0;i<=n-1;i++)
{
if(i==0)
ans=suf[2];
else if(i==n-1)
ans=ans*pre[n-1]/gcd(pre[n-1],ans);
else
ans=ans*gcd(pre[i],suf[i+2])/gcd(gcd(pre[i],suf[i+2]),ans);
}
printf("%lld\n",ans);
return 0;
}
```

Div1.B Problem and editorial by A.K.E.E.

**Editorial**

Tutorial is loading...

**Code**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
template <typename TYPE> inline void chkmax(TYPE &x,TYPE y){x<y?x=y:TYPE();}
template <typename TYPE> inline void chkmin(TYPE &x,TYPE y){y<x?x=y:TYPE();}
template <typename TYPE> void readint(TYPE &x)
{
x=0;int f=1;char c;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-'0';
x*=f;
}
const int MAXN=500005;
int n,k,a[MAXN];
bool solve()
{
readint(n),readint(k);
bool flag=0;
for(int i=1;i<=n;++i)
{
readint(a[i]);
if(a[i]<k)a[i]=0;
else if(a[i]>k)a[i]=2;
else a[i]=1;
if(a[i]==1)flag=1;
}
if(!flag)return 0;
if(n==1)return 1;
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n && j-i<=2;++j)
if(a[i] && a[j])return 1;
return 0;
}
int main()
{
int T;
readint(T);
while(T--)printf(solve()?"yes\n":"no\n");
return 0;
}
```

Div1.C Problem and editorial by A.K.E.E.

**Editorial**

Tutorial is loading...

**Code**

```
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int MAXN = 1005;
inline ll readint()
{
ll res = 0, f = 1;
char c = 0;
while(!isdigit(c))
{
c = getchar();
if(c=='-')
f = -1;
}
while(isdigit(c))
res = res*10+c-'0', c = getchar();
return res*f;
}
int n,m,T,a[MAXN][MAXN];
char str[MAXN];
int f[MAXN][MAXN],pos[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[MAXN][MAXN];
inline bool check(int x, int y)
{
for(int i = 0; i<4; i++)
{
int nx = x+pos[i][0], ny = y+pos[i][1];
if(a[nx][ny]==a[x][y])
return true;
}
return false;
}
pii q[MAXN*MAXN];
inline void bfs()
{
int front = 0, rear = 0;
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
if(check(i,j))
f[i][j] = 0, vis[i][j] = true, q[rear++] = mp(i,j);
while(front<rear)
{
pii now = q[front++];
for(int i = 0; i<4; i++)
{
int nx = now.fi+pos[i][0], ny = now.se+pos[i][1];
if(nx<1||nx>n||ny<1||ny>m||vis[nx][ny])
continue;
f[nx][ny] = f[now.fi][now.se]+1;
vis[nx][ny] = true;
q[rear++] = mp(nx,ny);
}
}
}
int main()
{
n = readint(), m = readint(), T = readint();
memset(a,-1,sizeof(a));
for(int i = 1; i<=n; i++)
{
scanf("%s",str+1);
for(int j = 1; j<=m; j++)
a[i][j] = str[j]-'0';
}
bfs();
int x,y;
ll t;
while(T--)
{
x = readint(), y = readint(), t = readint();
if(vis[x][y])
printf("%d\n",a[x][y]^(max(0ll,t-f[x][y])&1));
else
printf("%d\n",a[x][y]);
}
return 0;
}
```

Div1.D Problem and editorial by Rebelz

Part of solution by Elegia

**Editorial**

Tutorial is loading...

**Code**

```
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=998244353;
int n,m;
ll a[100005],ans[300005];
ll qpow(ll x,ll p){
ll ret=1;
for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
return ret;
}
int main(){
n=readint();
for(int i=1;i<=n;i++) a[i]=readint(),m+=a[i];
ll invm=qpow(m,cys-2),invn1=qpow(n-1,cys-2);
for(int i=m;i>=1;i--){
ll k1=i*invm%cys*invn1%cys,k2=(m-i)*invm%cys;
ans[i]=(k2*ans[i+1]+1)%cys*qpow(k1,cys-2)%cys;
}
for(int i=1;i<=m;i++) ans[i]=(ans[i]+ans[i-1])%cys;
ll res=0;
for(int i=1;i<=n;i++) res=(res+ans[m-a[i]])%cys;
res=(res+cys-ans[m]*(n-1)%cys)%cys;
printf("%lld\n",res*qpow(n,cys-2)%cys);
return 0;
}
```

Div1.E Problem and editorial by A.K.E.E.

**Editorial**

Tutorial is loading...

**Code**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
#define x first
#define y second
#define pb push_back
const int MAXN=200005;
int n,m,cnt;
pii a[MAXN];
bool avis[MAXN];
struct Range
{
int l,r,type;
ll val;
Range(){}
Range(int l,int r,int type,ll val):l(l),r(r),type(type),val(val){}
}b[MAXN];
int f[2][MAXN],wh[2][MAXN],res[MAXN];
vector<int> str[2][MAXN];
bool check(ll s,int l,int r)
{
if(s<=0)return !s && r-l+1>=0;
ll L=1,R=r-l+1,mid,ans=0;
while(L<=R)
{
mid=(L+R)>>1;
if((l+l+mid-1)*mid<=s*2)ans=mid,L=mid+1;
else R=mid-1;
}
return s*2<=(r+r-ans+1)*ans;
}
void modify(ll s,int l,int r,vector<int> &tar,int start)
{
ll L=1,R=r-l+1,mid,ans=0;
while(L<=R)
{
mid=(L+R)>>1;
if((l+l+mid-1)*mid<=s*2)ans=mid,L=mid+1;
else R=mid-1;
}
for(int i=l;i<=r;i++)
if(ans && (i+i+ans-2)*(ans-1)<=(s-i)*2 && (s-i)*2<=(r+r-ans+2)*(ans-1))
{--ans,s-=i;tar.pb(1);}
else tar.pb(0);
}
void update(int i,int ti,int ti_1,int pos,ll d)
{
if(pos<=f[ti][i])return;
str[ti][i].clear();
f[ti][i]=pos;wh[ti][i]=ti_1;
str[ti][i].pb(1);
for(int j=f[ti][i]+1;j<=b[i].r;j++)str[ti][i].pb(0);
modify(d-f[ti][i],b[i].r+1,f[ti_1][i-1]-1,str[ti][i],f[ti][i]);
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("code.in","r",stdin);
//freopen("code.out","w",stdout);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
ll y;
scanf("%lld",&y);
if(!y)continue;
a[++m]=make_pair(n-i+1,y);
avis[n-i+1]=1;
}
if(!m)
{
for(int i=1;i<=n;i++)putchar('0');
return 0;
}
sort(a+1,a+m+1);
b[cnt=1]=Range(a[m].x,a[m].x,2,a[m].y);
for(int i=m-1;i;i--)
{
if(a[i].y==a[i+1].y)b[cnt].l=a[i].x,b[cnt].type=0;
else if(a[i].y==a[i+1].y-1)b[cnt].l=a[i].x,b[cnt].type=1,b[cnt].val--;
else b[++cnt]=Range(a[i].x,a[i].x,2,a[i].y);
}
b[cnt+1]=Range(-1,-1,0,0);
bool error=0;
f[0][0]=-1;f[1][0]=n+1;
for(int i=1;i<=cnt;i++)
{
f[0][i]=f[1][i]=-1;
for(int t=0;t<=1;t++)
if(f[t][i-1]>b[i].r)
{
if(b[i].type==0 || b[i].type==2)
{
ll d=(b[i].val-1)-(b[i-1].val-1+t);
int pos=-1;
for(int j=min(b[i].l-1ll,d);j>b[i+1].r;--j)
if(check(d-j,b[i].r+1,f[t][i-1]-1)){pos=j;break;}
update(i,0,t,pos,d);
}
if(b[i].type==1 || b[i].type==2)
{
ll d=b[i].val-(b[i-1].val-1+t);
if(check(d-b[i].l,b[i].r+1,f[t][i-1]-1))
update(i,1,t,b[i].l,d);
}
}
if(f[0][i]<0 && f[1][i]<0 && i==cnt && !b[i].type)error=1;
}
if(error)
{
for(int t=0;t<=1;t++)
if(f[t][cnt-1]>b[cnt].r)
{
ll d=(b[cnt].val-1)-(b[cnt-1].val-1+t);
int pos=-1;
for(int j=min((ll)b[cnt].r,d);j>=b[cnt].l;--j)
{
if(avis[j])continue;
if(check(d-j,b[cnt].r+1,f[t][cnt-1]-1)){pos=j;break;}
}
update(cnt,0,t,pos,d);
}
}
int cur=(f[1][cnt]>0);
for(int i=1;i<f[cur][cnt];i++)res[i]=0;
for(int i=cnt;i>0;i--)
{
for(int j=0;j<(int)str[cur][i].size();j++)res[j+f[cur][i]]=str[cur][i][j];
cur=wh[cur][i];
}
for(int i=n;i>0;i--)putchar(res[i]+'0');
return 0;
}
```

Div1.F Problem and editorial by Rebelz

Hard version solution by Elegia

**Editorial for easy version**

Tutorial is loading...

**Code**

```
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=998244353;
int n;
ll fac[5005],inv[5005],d[5005][5005],ans[5005];
ll qpow(ll x,ll p){
ll ret=1;
for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
return ret;
}
int main(){
n=readint();
d[0][0]=fac[0]=inv[0]=1;
for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%cys;
inv[n]=qpow(fac[n],cys-2);
for(int i=n-1;i>=1;i--) inv[i]=inv[i+1]*(i+1)%cys;
for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) d[i][j]=(d[i-1][j-1]*(i-j+1)+d[i-1][j]*j)%cys;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ans[i]=(ans[i]+d[j][i]*inv[j])%cys;
for(int i=1;i<=n;i++) printf("%lld ",ans[i]*fac[n]%cys);
printf("\n");
return 0;
}
```

**Editorial for hard version**

Tutorial is loading...

**Code**

```
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=998244353,g=3,invg=(cys+1)/3;
int n;
ll ni[200015],fac[200015],inv[200015],tw[200005];
int mod(int x){return x>=cys?x-cys:x;}
ll qpow(ll x,ll p){
ll ret=1;
for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
return ret;
}
vi operator+(vi a,vi b){
vi ret(max(a.size(),b.size()));
for(int i=0;i<ret.size();i++) ret[i]=mod((i<a.size()?a[i]:0)+(i<b.size()?b[i]:0));
return ret;
}
vi operator-(vi a,vi b){
vi ret(max(a.size(),b.size()));
for(int i=0;i<ret.size();i++) ret[i]=mod((i<a.size()?a[i]:0)+cys-(i<b.size()?b[i]:0));
return ret;
}
vi operator*(vi a,ll b){
for(int i=0;i<a.size();i++) a[i]=1ll*a[i]*b%cys;
return a;
}
vi operator>>(vi a,int b){
for(int i=0;i<a.size()-b;i++) a[i]=a[i+b];
a.resize(a.size()-b);
return a;
}
namespace poly{
int N,l;
int A[1100000],B[1100000],r[1100000],pre1[20][600000],pre2[20][600000];
void pre(){
for(int i=1,r=0;i<(1<<19);i<<=1,r++){
int w=1,wn=qpow(g,(cys-1)/(i<<1));
for(int j=0;j<i;j++,w=1ll*w*wn%cys) pre1[r][j]=w;
}
for(int i=1,r=0;i<(1<<19);i<<=1,r++){
int w=1,wn=qpow(invg,(cys-1)/(i<<1));
for(int j=0;j<i;j++,w=1ll*w*wn%cys) pre2[r][j]=w;
}
}
void ntt(int *A,int N,int f){
for(int i=0;i<N;i++) if(i<r[i]) swap(A[i],A[r[i]]);
for(int i=1,r=0;i<N;i<<=1,r++){
for(int j=0;j<N;j+=(i<<1)){
for(int k=j;k<j+i;k++){
int x=A[k],y=1ll*A[k+i]*(f>0?pre1[r][k-j]:pre2[r][k-j])%cys;
A[k]=mod(x+y); A[k+i]=mod(x+cys-y);
}
}
}
if(f<0){
int invn=qpow(N,cys-2);
for(int i=0;i<N;i++) A[i]=1ll*A[i]*invn%cys;
}
}
void init(int t){
N=1,l=0;
for(;N<t;N<<=1) l++;
for(int i=0;i<N;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
}
void getmul(){
ntt(A,N,1); ntt(B,N,1);
for(int i=0;i<N;i++) A[i]=1ll*A[i]*B[i]%cys;
ntt(A,N,-1);
}
vi mul(vi a,vi b){
init(a.size()+b.size());
for(int i=0;i<N;i++) A[i]=i<a.size()?a[i]:0;
for(int i=0;i<N;i++) B[i]=i<b.size()?b[i]:0;
getmul();
vi ret(a.size()+b.size()-1);
for(int i=0;i<ret.size();i++) ret[i]=A[i];
return ret;
}
vi polyinv(vi a,int l){
if(l==1) return vi(1,qpow(a[0],cys-2));
a.resize(l);
vi b=polyinv(a,(l+1)/2);
init(l<<1);
for(int i=0;i<N;i++) A[i]=i<l?a[i]:0;
for(int i=0;i<N;i++) B[i]=i<(l+1)/2?b[i]:0;
ntt(A,N,1); ntt(B,N,1);
for(int i=0;i<N;i++) A[i]=1ll*A[i]*B[i]%cys*B[i]%cys;
ntt(A,N,-1);
vi t=b*2;
t.resize(l);
for(int i=0;i<l;i++) t[i]=mod(t[i]+cys-A[i]);
return t;
}
vi polydiff(vi a){
for(int i=0;i<a.size()-1;i++) a[i]=1ll*(i+1)*a[i+1]%cys;
a.resize(a.size()-1);
return a;
}
vi polyint(vi a){
a.resize(a.size()+1);
for(int i=a.size()-1;i>=1;i--) a[i]=1ll*a[i-1]*ni[i]%cys;
a[0]=0;
return a;
}
vi polyln(vi a,int l){
return polyint(mul(polydiff(a),polyinv(a,l)));
}
vi polyexp(vi a,int l){
if(l==1) return vi(1,1);
a.resize(l);
vi b=polyexp(a,(l+1)/2);
init(l<<1);
vi t=mul(b,vi(1,1)-polyln(b,l)+a);
t.resize(l);
return t;
}
vi polypow(vi a,int l,int k){
return polyexp(polyln(a,l)*k,l);
}
}
vi getans(){
vi f(0);
for(int i=0;i<=n+1;i++) f.push_back(inv[i+1]);
vi tmp=poly::mul(f,poly::polyinv((vi(1,1)-f)>>1,n+1));
vi tw(0); tw.resize(n);
for(int i=0;i<n;i++) tw[i]=tmp[i+1];
vi h(0);
h.push_back(1),h.push_back(1);
h=poly::polyinv(poly::polyln(h,n+3)>>1,n+2);
vi g=poly::polyinv((vi(1,1)-h)>>1,n+1);
vi ph=poly::polypow(h,n+1,n+1);
vi t1=poly::mul(g,ph);
vi tmp1=poly::mul(poly::polydiff(h),ph);
tmp1.resize(n+1);
vi tmp2=poly::mul(g,g);
tmp2.resize(n+1);
vi t2=poly::mul(tmp1,tmp2);
for(int i=0;i<n;i++) tw[i]=mod(tw[i]+cys-ni[n+1]*mod(t1[i+1]*(n-i+1)%cys+t2[i+1])%cys);
for(int i=0;i<n;i++) tw[i]=tw[i]*fac[i]%cys;
reverse(tw.begin(),tw.end());
tmp.clear();
for(int i=0;i<n;i++) tmp.push_back(i&1?cys-inv[i]:inv[i]);
tw=poly::mul(tw,tmp);
tw.resize(n);
reverse(tw.begin(),tw.end());
return tw;
}
int main(){
poly::pre();
n=readint();
fac[0]=inv[0]=1;
for(int i=1;i<=n+5;i++) fac[i]=fac[i-1]*i%cys;
inv[n+5]=qpow(fac[n+5],cys-2);
for(int i=n+4;i>=1;i--) inv[i]=inv[i+1]*(i+1)%cys;
for(int i=1;i<=n+5;i++) ni[i]=inv[i]*fac[i-1]%cys;
vi res=getans();
for(int i=0;i<n;i++) printf("%lld ",res[i]*fac[n]%cys*inv[i]%cys);
printf("\n");
return 0;
}
```

You can also view Div1.F editorial by Elegia here: https://codeforces.com/blog/entry/77280

Anyway, hope you like these problems and thank you for participating!

Video Editorial for Div2C/Div1A

Thanks for the video tutorial! What is the time complexity of calculating the prime factors of n numbers in your code for Div1A?

$$$O(VALMAX$$$ $$$log$$$ $$$VALMAX)$$$, where $$$VALMAX$$$ is the biggest value in input

Simple solution for the problem.

stefdasca you being an orange we expect you to upload atleast Div1B&C

I never upload right after contests video solutions for tasks I'm not 100% sure I used a right approach.

As an example, I FST'd Div1B yesterday, it's easy to guess what everyone would've told me in comments soon after if I uploaded them.

Also, I'm planning on doing video tutorials for more problems, but I don't have much time for this right now.

lol pupils should not expect anything. He can solve that , he just makes them for lower rated people like you , so you should thank him.

go corona corona go.

CP_Sucks u dont know me so better dont judge me and dont tell me whom to thank and whom to not Aur phir bhi maa chudwani h to bata de

By any chance you are from pakistan ?

i kept on MLEing for problem C, although I had the right idea the whole time (i think I did solution 2). What can you use to calculate the number of pairs besides stuff like maps and arraylists, which MLE? Is there a way to do it in o(1) space? I literally spent 1h30m trying to implement this goddamn problem and I don't think I've ever been more tilted at a codeforces problem than this one https://codeforces.com/contest/1350/submission/80029151

Maybe you can try only storing 2 least elements like what I did. https://codeforces.com/contest/1350/submission/79859241

omfg im retarded LOL

Thanks for Editorial. Really enjoyed the contest. Problems were very interesting

Video editorial Div2 B

Video editorial Div 2C/Div1A.

Video Editorial Div2D/Div1B

Thank you!

Great approach.

Thank you, very informative one. Is it some identity or you found the pattern during the contest?As I was struggling to find sets s1,s2,..sn.

can you please tell how to recognize that Div2 B was a DP problem?Thank you in advance.

well i am not a experience programmer though i think i can help here you. See we are going throught multiple of each index because that is the only value which is divisible by particular index. Now supoose we start from index 1 then all multiple would be n — 1 because 1 can divide all of them so we will fist save all the answer which satisfy the condition that is arr[i] > arr[1]. Now when we move to second index then multiple of 2 would be 4,6, 8.... and so on so if any of these indces satisfy the condition then the answer would be update by the previous answer from index 1 + current index and new index would be added too in this. So here we can clearly see that previosly we saved answer when we checking of 1's multiple and now we got new index which is divided by 2 so these sequence would looks like 1, 2, and multiple of 2 where 1, 2 sequecne answer was already saved in dp. Sorry for any grammatical mistake can't give time to correct it any try to find something else if it can't help. I could try only.

In the tutorial u have devided 10*8 by their GCD which returns the GCD of the LCMs of the pair. How it works , please? striver_79

Read about GCD's associative property or checkout the comments in my video, I have given an explanation.

Good problems!

nice problem set (math)

can anyone plz check my solution for div2 C (it got accepted during contest) https://codeforces.com/contest/1350/submission/79877064

in line 54 :: if(t%j==0) {div[j]++;div[t/j]++;}

if j == t/j in this case you are adding twice.

༼ つ ಥ_ಥ ༽つ thanx

a lot of people did same mistake

Anyone else solved D1C using bitsets?

Bitsets are expected to get MLE or something else. I'm curious about your solution XD

Well, I have noticed two facts (both are proven in the editorial):

So I have written a function

`go(vector<bitset<1000>>)`

which would give me the next iteration in $$$O(nm/64)$$$. It is not hard to come up with a boolean function $$$f(val, l, r, d, u)$$$ which would give the result for the next iteration ($$$l, r, d, u$$$ are the values of adjacent cells). So using this formula we can compute a whole row in $$$O(m / 64)$$$.Now for queries with $$$p < n + m$$$ I use scanline, and for $$$p \geq n + m$$$ I look at the parity. So I am using $$$O(nm(n + m)/64 + t)$$$ time and $$$O(nm/64 + t)$$$ memory.

Cool approach.

Another good Chinese round !

In d1C, you have used grid instead of cell, for example

Fixed.

save some problems for IMO

I would like to give an interesting (although not optimal in terms of complexity) for div 1C. Observe the grid will "converge" to a length 2 cycle after some sequence of move. You can find that out by writing a brute force solution. With some guessing, you may find out you need around $$$n + m$$$ steps to converge, which is consistent with the results in the editorial.

The problem is brute force requires $$$O(nm(n + m) + t)$$$ which is too slow. However, we can speed up the brute force using bitset. By shifting and some bitwise operations, you can check and update the whole row at once. You may also want to use the rolling array technique to fit in the memory limit. The complexity after optimization would be still $$$O(nm(n + m) + t)$$$ but with an additional $$$1/64$$$ constant, which should fit in the time limit.

79891996 The solution is badly written, serve as proof-of-concept only.

Fun fact: with the rolling array technique, my solution uses less memory compared to most solutions.

Yeah, we once wanted to hack your solution by modifying ML or TL, but in the end we decided to only hack bitset solutions which are bad at optimizing their memory usage.

I don't really think you can hack this solution (not hacking a lot of bfs solutions). In my experience $$$O(n^3/64)$$$ works noticeably faster than $$$O(n^2logn)$$$ for $$$n = 5000$$$.

Could you explain a bit about what you mean by

rolling array technique? Prelimnary google search doesn't seem to give anything worthy.I think it's somewhat the same as scanline. You can understand it this way: you only need to store one iteration at a time. When you've answered all the queries for the iteration $$$p$$$, you can change it to the iteration $$$p + 1$$$.

Okay, got it. Had a look at STommydx solution. Basically offline query sorting, till N+M maximum. interesing, thanks.

What does this sentence means?

Nice editorial but i wish your English was stronger :(.

Suppose $$$S_x$$$ is the set containing all the situations that the game end up with all biscuits in $$$x$$$-th player. And for a situation $$$t$$$ we denote its probability to occur by $$$P(t)$$$ and its time by $$$L(t)$$$, then $$$E_x = \sum\limits_{t\in S_x} P(t)L(t)$$$.

Thanks for the comment, now i understand it better(you can use "multiplied by" instead of "times").

Can someone explain Div1D better? Thanks

I used Generating Function to solve, and maybe it is easier to come up with such solution if you are familiar with them.

First, let $$$a_k$$$ be the probability of ending the game in $$$x$$$ turns. Let $$$A(x) = \sum_{k=0}^{\infty} a_kx^k$$$. Then our objective is to find $$$A'(1)$$$, where $$$A'(x)$$$ is the derivative of $$$A(x)$$$.

Finding $$$A(x)$$$ is hard, so we should multiply something to it to make it easier. From now on, for convinience, assume the game doesnt end even if the ending state occurs. Let $$$b_k$$$ be the probability of one person having all biscuits after moving k steps starting with the state, and that

it is the first time that person owns all biscuits. Let $$$B(x) = \sum_{k=0}^{\infty} b_kx^k$$$. Then, we realise that $$$A(x)B(x)$$$ is the generating function of the sequence $$$c$$$, where $$$c_k$$$ is the probability of a person owning all biscuits after $$$k$$$ steps (also enforce that it is the first time that person owns all biscuits). Denote generating function of $$$c$$$ as $$$C(x)$$$.$$$C(x)$$$ can be written as sum of $$$D_i(x)$$$, where $$$D_i(x)$$$ is the generating function for the sequence $$$d_{i,k}$$$, where $$$d_{i,k}$$$ is the number of ways to reach the state that $$$i$$$-th person has all the biscuits the first time after $$$k$$$ steps. Consider $$$D_i'(1)$$$, which is the expected number of steps to reach that state. We realise that this value is only dependent of $$$a_i$$$, $$$n$$$, and total number of biscuits. Let's denote it as $$$e_{a_i}$$$. The $$$e$$$ forms a Markov Chain and you can solve all values of $$$e$$$ in $$$O(n*log)$$$.

Finally we will go back to the original formula, $$$A(x)=\frac{\sum_{k=1}^{n} D_k(x)}{B(x)}$$$.

Then, $$$A'(1)$$$

$$$= \frac{B(1)(\sum_{k=1}^{n} D_k'(1))-(\sum_{k=1}^{n} D_k(1))B'(1)}{B(1)^2}$$$

$$$= \frac{n(\sum_{k=1}^{n} e_{a_i})-n(n-1)e_0}{n^2}$$$

Which can be computed in $$$O(N)$$$ easily.

This is pretty much the same as the editorial.

is there a non generating fn solution, or pretty much you have to calculate a derivative to get the formula?

Can you help me with the formula for calculating e, I found Markov chains, but we have 3 * 10 ^ 5 different states, and this is a fairly large matrix, please

There is a 1200 points difference between problem C and problem D. Has anyone seen a bigger difference?

See this

Codeforces Global Round 2

How can i forget the May Challenge Div2 on Codechef ;-)

Tricky Questions

I was fooled by the D2/B problem into n^2 DP, it blocked my mind.

After that my confidence was lost and finally I gave up. and now I found out how iterations were made in the problem and it is kind of like the iteration we make while sieving primes.

Overall, it is depressing!!!

Still, I would like to appreciate and say very good work done by the writer!!

Thank you for the contest

You may feel depressed but you learned something. When you come upon this pattern again, you will find it and solve a problem. Good luck in the future!

solution link

I used the n*sqrt(n) but I got WA on pretest 2 could you please tell me where I went wrong ? sxpg

You should initialize count[] with all values equal to 1 instead of 0. j<=sqrt(i) is risky too, sqrt gives float and can create numerical errors. For example, sqrt(25) might give 4.999999997 instead of 5 and you will miss this 5. Better to check j*j<=i

thank you, after making those changes I got AC.

bro that hint even helped me....Now i initialized the array with 1 and its accepted. Just one change...!! That had put me into depression in live contest ;-<

Another interesting solution for Div2 C is that the value of gcd of all pairs whose smallest index is 'i' is LCM(a[i], GCD of all a[j] s.t. j>i). The value corresponding to "GCD of all a[j] s.t. j>i" can be easily found using suffix array.

You can use prefix gcd https://codeforces.com/contest/1349/submission/79820899

Wow, that's elegant! Could you explain how this works? Not sure how to intuitively think about it in terms of prefix gcd.

It's the same as jatinmunjal2k's solution but with < instead of > so prefix instead of suffix.

In div2C, doing what is said in the problem statement sufficed: 79904920 (After the contest but I am kind of disappointed)

please insert sample code of editorials , that will be better to understand .

Done. :)

Can someone explain the transformation formula in easy, mostly non mathematical, words?

$$$f_i = \max\limits_{j\mid i, s_j<s_i} {f_j + 1}$$$

It means maximum $$$f_j$$$ for all $$$j$$$ such that $$$j$$$ divides $$$i$$$ and $$$s_j < s_i$$$.

Good problems(especially div2d)! Thanks!

Div 2Bcan't we solve 2B by making an unidirectional graph and finding the longest path?? can anyone see my solution and tell me what's wrong with that.(my submission 79865870)We can, your solution doesn't calculate longest path correctly, i prefer using dp to calculate it.

can you please tell me what's wrong with my longest path calculation.

I think the check of the vis[] in dfs function is not correct. It should be removed there, allways traverse into child node.

DFS would work, but not with a "visited" array. You DFS from 2 and tick 12 as visited, then you DFS from 3 and miss an edge from 3 to 12.

Also, I think it might get TLE.

If you are curious how to use DFS for it, run a DFS and if you are at vertex $$$v$$$, then set $$$ans_v$$$ equal to $$$\max\limits_{u\,\in\, g_v} ans_u + 1$$$, do it right before ending the function.

I read in a book "Algorithms" by Dasgupta & Papadimitriou that every DP problem has a directed acyclic graph hidden in it. This is a nice example!

Can anybody explain what that weird symbols mean in Div.1 B solution?

$$$∃$$$ means for at least one $$$\dots$$$

$$$∀$$$ means for all $$$\dots$$$

For example $$$∀ 1 \le i \le n, a_i = 0$$$ means for all $$$i(1 \le i \le n)$$$, $$$a_i$$$ is equal to 0 but $$$∃ 1 \le i \le n, a_i = 0$$$ means for at least one of $$$i(1 \le i \le n)$$$, $$$a_i$$$ is equal to 0.

Why is this approach wrong for Div2B? int dp[n+1]={0}; dp[1]=1; for(i=2;i<n+1;i++) { vectorv; for(j=1;j*j<=i;j++) { if(i%j==0) { v.pb(j); if(i/j!=j) v.pb(i/j); } } for(j=0;j<v.size();j++) { if(a[v[j]]<a[i]) { dp[i]=max(dp[i],dp[v[j]]+1); } } } int mx=0; for(i=1;i<n+1;i++) { mx=max(dp[i],mx); } cout<<mx<<"\n";

Change

`int dp[n+1] = {0};`

to`int dp[n+1] = {1}`

:), also i prefer to iterate over $$$n+1$$$ and set $$$dp_i$$$ equal to 1 by myself.thank you sir

I know I was a little rude, but "Read problem statement" kinda pissed me off. Apologies to the setters but the statement was definitely ambiguous.

Div1 C "no adjacent cells with the same color as this cell", I interpreted initially as any two adjacent cells anywhere in matrix. It said nothing about "no adjacent cells to it".

If you felt uncomfortable with that, we're sorry. But it is a quick option to answer a question, just like Yes, No, No comments and Question is unclear. So I think the usage of this is also reasonable.

+1

English is bad in this statement. Simply writing "A black cell changes if at least one of its adjacent cell is black and same for white." was sufficient.

Even after understanding it, I had to read it whenever I need this condition while writing soln.

That's so true. Although the meaning of the statement (condition) is clear for me, I messed up the condition multiple times while debugging. I have to reread the condition again and again just to make sure I did not think in the opposite way.

Alternatively for Div 2D/1B, you can try finding any subarray of length at least 2 which has median at least k. If you can find such a subarray, the answer is yes, otherwise it is no. FatalEagle describes how to find number of subarrays with median at least k over here.

.

I have a very short solution for Div1A which I think is pretty cool, and is not mentioned in the editorial. It uses dp:

https://codeforces.com/contest/1350/submission/79884445

Basically the idea is we can keep track of what primes are divisors of all the numbers, and which primes are divisors of at least n — 1 of the numbers. The first row in dp is just the gcd of all the numbers, thus the primes which divide all numbers. The second row of dp is the factors which divide at least n — 1 of the numbers. The first row is easy to calculate, its just the gcd of the current number in the array and the previous column in dp. The second row is a little trickier, but it's just the lcm of the primes which divide all the previous numbers and the gcd of the previous column and the current number in the array. The code itself might explain it a bit clearer.

But basically to get the primes which divide at least n — 1 of the numbers, you can either just take all the primes which divide all of the first n — 1 numbers, or take the primes which divide at least n — 2 of the first n — 1 numbers, and also divide the current nth number, which is how you get the second equation. The final answer after iterating through dp is then just the cell in the second row and last column.

The code itself might make what I'm saying a bit clearer. If this is confusing I can try to explain it better, but I thought this was a pretty neat solution.

Your explanation is pretty clear than editorial for me but it's a little confusing how you are getting factors that divide n-1 numbers. It would be highly appreciable if you explain it with an example :)

Ok let me try:

So for example let's use one of the pretests:

10 24 40 80

We start by assigning dp[0][1] = gcd(10,24) and dp[1][1] = lcm(10,24), as the gcd gives all the primes which divide both, and lcm gives all the primes which divide at least n-1 (so in the case of the first two numbers, we include any prime which divides either). So now we have dp[0][1] = 2 and dp[1][1] = 120.

Now, calculating dp[0][2] is easy, it's just gcd(dp[0][1], 40) = 2. This is all the factors which divide all of the first three numbers. Now we calculate dp[1][2] by lcm(dp[0][1], gcd(dp[1][1], 40) = lcm(2, gcd(120,40)) = lcm(2, 40) = 40. So here we have that all the prime powers of 40 divide at least (3-1)=2 of the first 3 numbers (when I said factors in the previous post its actually slightly incorrect, as we in fact need any prime power factor to divide at least n-1 of the numbers, not any factor).

The reason we did lcm(2, gcd(120,40)) is the following: 2 divides all of the first 2 numbers, so it trivially divides (3-1)=2 of the first 3 numbers. On the other hand, prime powers in 120 divide at least (2-1)=1 of the first 2, and so if we take gcd(120,40), the result will also divide 40, and so we get prime powers which divide 1+1=2 of the first 3 numbers. We then take the lcm of these two cases, as any prime power which satisfies either is good.

Finally, for 80, we have dp[0][3] = gcd(2,80) = 2, and dp[1][3] = lcm(2, gcd(40,80)) = lcm(2,40) = 40, and that is in fact the desired answer.

Let me know if this helped :).

Wow, the thing in which I was confused is very well explained in the last second paragraph. Thanks to you. Hope you achieve high ratings in future :)

Thanks :). Same for you.

please can you explain, i am a new coder, why we are seeing primes when the question is all about GCD and LCM

GCD and LCM intrinsically have to do with primes, because they are comparing factors of numbers. GCD takes the minimum power of every prime from the two numbers, and LCM takes the maximum power of every prime.

ohh i got it now, thanks

A (maybe) more intuitive way for Div1 D:

Let $$$m=\sum a_i$$$. Like this problem, if we find a potential function $$$f$$$ such that $$$\mathbb{E} \left(\sum_{i} f(a_i) \right)$$$ decreased by $$$1$$$ in each step, the answer is simply $$$\sum f(a_i)-\left((n-1)f(0)+f(m)\right)$$$, which is the initial potential minus the final potential.

To satisfy the condition, we have equaltion

, which is

.

It is not hard to see if

for all $a (0\leq a \leq m-1)$, it satisfies the condition.

So simply solve these equations and find the answer.

We can use this method for all problems of this kind.

The last term is missing a factor of $$$m$$$.

Thx, fixed.

Deleted.

Deleted.

Sorry for necroposting. But what are the boundary conditions for f ? Like how do we solve the above recurrence for f without any boundary conditions ?

Div 2C/ Div 1A

observe --> lcm[a, gcd(b, c)] = gcd(lcm[a, b], lcm[a, c]) and gcd(a, lcm[b, c]) = lcm[gcd(a, b), gcd(a, c)]

using them the sought answer can be brought down to

`gcd of { (a[i]*gcd(a[i+1], a[i+2], .. , a[n]))/gcd(a[i], a[i+1], .. , a[n] | i <= n }`

so create an array (say, g) as: g[i] = gcd(a[i], a[i+1], .. , a[n])

check code here: 79893540

Hi can you explain to me why did you divide the above numerator by gcd of all elements ??

that is not gcd of all elements, that is the gcd from a[i] to a[n]

that came only because i converted lcm to gcd, final answer in lcm form -->

`gcd of { lcm(a[i], gcd(a[i+1], a[i+2], .. a[n]) | i <= n }`

Oh ok my bad .. nice

Anyway, problemset was nice — thx a lot

a good contest based on number theory. :)

Guys, could you please find the problem in my solution. By my mind it should work. Problem

Div.2E-Div1.C.My solution

I will be thankful.

Don't waste time, sorry, I think I found the problem(

Here is my O(

NlogN) solution to Div2C/Div1C. Code.I counted the number of instances of each number in a count array mp. Then for each i=2 to 2e5, I have counted the number of elements in the sequence which are divisible by i in cnt[i]. Now if we observe the final gcd will have contributions only from numbers i which have

`cnt[i] >= n-1`

. (They appear in at least one of the numbers for each pair.) Now since it may happen than cnt[2] = n and cnt[4] = n-1. In that case we may ignore 2's contribution by taking a lcm of the current answer with 4 and similarly for higher powers of a prime. I hope I didn't make it complex for anyone :P.How is it

NlogN? the loop in which I update cnt runs for(N + N/2 + N/3 + ... 1)which gives NlogN. Thanks to ffao for correcting me and Everule for the explanation why it isNlogN. Also sequential addition of Nlog(N) for calculating the cumulative gcd.(N + N/2 + N/3 + ... 1) is O(N log N), not O(N log log N).

I'm sorry, but that is not true. Sieve of Eratosthenes is $$$O(n\log{\log{n}})$$$ because it is $$$\sum N/p$$$ for all primes less than $$$\sqrt{n}$$$. $$$\sum_{i=1}^{N} N/i \approx n\log{n}$$$

Div2 C/ Div1 A Can someone please check why my code is getting TLE. I think it should be within the bounds. My Code : 79910710

It overflows, you wrote

`for(int j = i*i; j <= n; j += i)`

, then $$$j$$$ will overflow.No, j <= N and N = 1e5. So, it shouldn't overflow

Lets say $$$i = 10^5$$$, then it calculated $$$j = 10^5 \ctod 10^5$$$, which overflows and goes to negative numbers(for example $$$j = -10^9$$$), you see? it overflows AF. :)

can someone tell whats wrong in my approach for A 79887649

For big prime numbers, your fact is equal to -1(should be equal to $$$n$$$ itself).

I cannot get my head around div2C / div1A. Can someone explain it using some examples? Pretty please.

Since array t contains all possible lcm of pairs from input, We just need to find the product of all prime numbers that divide atleast n-1 numbers of the input. A simple example would be [5 7 5 5], You can notice that every element in the array t would have 5 as one of its divisors since 7 cannot exist alone.

Now lets take an example from sample test to see how the algorithm works:

input= [10 24 40 80] , n=4 ans=1; //initially

Code

By f=2 you're checking if there are any two numbers not divisible by i ,but i did reverse I checked if there are n-1 numbers or not which are divisible by i and I got TLE. You just ended up getting lucky with test cases in this contest :P

Not really, It's pretty obvious that if you loop n-1 times inside a loop of 2e5 its gonna be TLE. Notice how i have a break statement if 2 numbers are not divisible by i, The one thing to be noticed is 2e5 can only have a max of 17 prime divisors(2^17=1.3*1e5) so the inner loops only gets traversed around 17*2 times in worst case but for your code it gets traversed every time. It's not a coincidence that my solution got accepted in 62ms :)

Hey, thanks!

Thanks for the problems!

By the way, a formal petition to the setters to write cleaner model solutions than this:

I mean, I can probably understand it, but please, you've got plenty of time before the contest. You aren't racing to write the models as fast as possible, and you can spend some of your time making sure the code is readable.

For problem Div2 D once checkout my solution 79912071 I have not checked a case when n = 1, I simply wrote if(n==1) cout<<"yes", but my doubt is there should be if(n==1&&arr[0]==k)cout<<"yes";

You first checked that the array has at least one $$$i$$$ such that $$$a_i = k$$$, so if $$$n == 1$$$ then you can be sure that the only element is $$$k$$$.

Ya, i got it thanks

If arr[0] was not k, ok would remain false and hence it will not even reach the case:

if(n==1) ..

Why is this approach wrong for

Div2B? I tried brute force and generated all sequences of divisors and then checked whether the sequence is beautiful or not. I am getting WA on pretest 2: link to submissionIts $$$2^n - 1$$$ beautiful sequences in worst case(if array $$$s$$$ is increasing) so i guess your brute force should not work, try solving it with DP.

Sorry, its not $$$2^n-1$$$ sequence, your brute force should work, sorry for that.

Editorial should be in little simple language. Can anyone please explain Div 2 Problem C?

But there is complete editorial... What didnt you understand?

Having trouble wrapping my head around one line of this problem: 1349A — Orac and LCM. In the proof, the editorial says: "Proof. if there are at most n−2 integers in a that s.t. pk∣ ai, there exists x≠y s.t. pk∤ax and pk∤ay, so pk∤lcm({ax,ay}) and pk ∤ ans."

I can't seem to convince myself why if pk does not divide ax and ay, it must not divide lcm({ax,ay})? Would appreciate any insight, thanks!

Let me proof it, if we have at most one $$$a_i$$$ such that $$$p$$$ doesn't divide $$$a_i$$$, then for any $$$1 \le i,j \le n$$$, $$$p$$$ divides $$$lcm(a_i, a_j)$$$(because at least one of them is dividable by $$$p$$$). So $$$p$$$ divides the answer as well.

Thanks for the reply! That makes sense to me. Its more so the contrary that I don't really get. When they consider the case where you have two entries ax and ay that are not divisible by p, why can we say that p does not divide lcm({ax,ay})?

Lets prove :

Lets say the smallest number such that it doesn't divide $$$x$$$ and $$$y$$$, but divides $$$lcm(x, y)$$$ is $$$p$$$, with use of math you can see that $$${lcm(x, y)\over p}$$$ is dividable by both $$$x$$$ and $$$y$$$, but its smaller than $$$lcm(x, y)$$$ the contradiction prove that no such $$$p$$$ exist that doesn't divide $$$x$$$ and $$$y$$$ but divides $$$lcm(x, y)$$$.

Another proof :

Lets say such $$$p$$$ exist, we know that $$$lcm(x, y)\cdot gcd(x, y) = x\cdot y$$$, as $$$p$$$ divides $$$lcm(x, y)$$$ so the left part is dividable by $$$p$$$, but the right part is not, the contradiction proves no such $$$p$$$ exist that doesn't divide $$$x$$$ and $$$y$$$ but divides $$$lcm(x, y)$$$.

Appreciate the proofs, thanks again. One final follow up. The example below is what it is tripping me up a bit. Do you know what is wrong about it? So lets say we have x = 3 and y = 10. Then lcm(3,10) = 30. Lets say p = 15. Then 15 does not divide x and does not divide y, but does in fact divide lcm(x,y). Is there something that does not work here?

It doesn't work because 15 is not prime. The statement applies to primes. If there is a prime (or prime power) which doesn't divide x and doesn't divide y, it won't divide lcm(x,y). We only care about prime powers in the solutions to the problem, so this is sufficient.

I considered $$$p$$$ to be prime(or a power of a prime) but forgot to add it in the proof. Sorry.

All good, thanks for the explanation!

i dont understand what does this mean "p^k | ans" why we are doing OR operation here??..

It means $$$p^k$$$ divides $$$ans$$$.

thanks

hey thank you so much

Solution explanation for Div 2. C. Orac and LCM -> Find Gcd of whole array say g -> Divide whole array by g -> Now array is suppose x1,x2....xn -> For some index k lets find the value for all its pair.. -> so, vk= gcd(lcm(x1,xk),lcm(x2,xk).....lcm(x(k-1),xk),lcm(x(k+1),xk),.....lcm(xn,xk)) -> xk val is included in all terms we can take it as common -> vk = xk*gcd(x1,x2,..x(k-1),x(k+1),......xn); -> no harm in taking xk as common as the second term is coprime to first one always... as we have already divided all the numbers by total arrays gcd. -> simply take gcd of all vi's for all 1<=i<=k Here is my solution for reference https://codeforces.com/contest/1350/submission/79831074

can anybody please tell me why my solution is wrong.Div2/prob b pretest2 failed. https://codeforces.com/contest/1350/submission/79864654

Can someone please check my solution. It's giving wrong ans I'm doing exactly as editorial it a O(n*sqrt(n)) solution https://codeforces.com/contest/1350/submission/79914083

your find_divisors function is wrong. check for array of size 24 are you getting right divisors for the number 24. Probably you shouldn't divide the number(whose divisor you are finding) by the divisor.

My approach for Div1A/Div2C is similar to the Second approach discussed in the editorial, but I am unable to understand the first approach, can anyone help me telling how we arrive at the given observation mathematically?

https://codeforces.com/contest/1350/submission/79817740 This solution got accepted for A. Orac and Factors though its Time Complexity is O(k) and k is 10^9 .... How is this possible ????

Compiler helped him much, I think.

lol am I the only who thought in div 2 B they were talking about values stored at indexes to be divisible instead of indexes

I have implemented the solution to

Div2E/Div1Chere https://codeforces.com/contest/1350/submission/79919196. Why am I getting runtime error and this weird verdict? I have been trying to debug my code but cannot find anything. If someone can please help it will be really great.Edit:I've got it now, I was accessing q.front() by reference and then popped it. This was causing the RE.Hey guys,

I would like to give me some tips to improve. Specifically, on problem D, during the contest, I observed that if we somehow can form 2 numbers equal to k in row, then we can easily convert the entire sequence to k. In order to do that, we can go to each k and look left or right and see if we can have it as a median. If we can't do it (or no such k exist), the answer is no. Otherwise, obviously, there is a way! Please refer to my submission here for more details on my approach: https://codeforces.com/contest/1350/submission/79882933

Then, I was getting always WAs on test 10 and I was scrambling to find a case where it didn't work, but I couldn't find what's wrong and eventually I missed the problem. After that, I saw a solution and it hit me; if we have 2 consecutive numbers that are greater than k or 2 that have a smaller number in between, we can propagate it until it reaches a k and after that we can use my approach to finish it.

Unfortunately for me, I was unable to see this case and I did not solve the it. I would like your opinion on those 2 stuff:

What is the easiest way to generate test cases that could help on finding the flaws on a speculation like the one I presented above? For me, it's quite difficult to find any (especially on this problem).

On this case, was there any other way to disprove a speculation other than trying to find a counterexample?

Any help appreciated! Thanks!

Use Python to generate many small cases and a brute force solver. Though it might be hard for graph problems. But for problem D, this can be made. I do this when I can't figure out what's wrong and it also helps reduce WA's. It's not that hard to do this because Python has many tools like

`itertools`

and easy syntax for many stuff. When you find a small case, you can easily trace what's wrong.As for Div1F2:

0) I assume that

(The formula in the editorial misses $$$F$$$ in the numerator in the first term on the right hand side?)

1) What is "Lagrange Inversion"? I've never heard about this, and wikipedia suggests this or this; both of them are said to be called "inversion formulas", but I'd guess the former somehow applies? How to apply it, then? I can't see how to get the equation just below

"And from the Lagrange Inversion: (...)"from the one above.2) How to even think about this solution? To me, it looks like magic or a bunch of random complicated transformations of a power series which suddenly stops at a formula that we can compute using FFT fairly easily. I can even trace the steps (apart from this Lagrange inversion I mentioned before) and verify they're roughly okay. But... I can't picture myself ever finding out I needed to follow exactly these steps, even if I spent a month on the problem -- even after having read the editorial. What should I do if I want to get some understanding on what happens here?

I am little confused about the formula in easy version is the code correct or does the editorial formula has some extra terms, because, if compared directly some factorials are missing in the code.please correct me if I am wrong. The d_ij version has y! But code doesn't.

The meaning of $$$d_{i,j}$$$ in the code is different than in the editorial. This code follows the editorial exactly.

(and extension to F2 is here)

General Lagrange inversion formula is, for $$$F(x)$$$ and $$$G(x)$$$ satisfied $$$F(0)=G(0)=0,\,[x^1]F(x)\neq 0,\,[x^1]G(x)\neq 0,\,G(F(x))=x$$$:

Do you know any good source to learn more about the general Lagrange Inversion Formula (even if it's Chinese explanation)?

So in the editorial we substitute $$$F(x) = w(x)$$$, $$$G(x) = \frac{x}{\phi(x)}$$$ and $$$H(x) = \frac{1}{1 - \phi(x)} \cdot \frac{1}{1 - ux}$$$ I guess?

It's mentioned in the books "Enumerative Combinatorics", "Analytic Combinatorics" and "Generatingfunctionology".

Elegia

We know that $$$\phi(x)=\frac{x}{\ln (1+x)}$$$. Then

so $$$H(x)$$$ also contains a term of $$$\frac{1}{x}$$$, meaning that it is not analytic. Doesn't Lagrange Inversion require $$$H(x)$$$ to be analytic (according to Wikipedia)? Though I'm not familiar with this at all ...

This theorem can be proved in formal Laurent series, it means that it's always correct when $$$n, k\in \mathbb Z$$$:

It seems that indeed a $$$F$$$ was missed. And, to be frank, I'm also not so good at these things. Elegia has an indepth study in combinatorics, generating functions and skills behind, and he has top level of skills in GF-oriented counting problems, even among those top OIers in China. This solution is his masterpiece, maybe.

What does GF mean? And what is olers?

Generating function

OI — Olympiad in Informatics

OIer — participant of such Olympiads

Same here, I'm trying to find the logic behind this. Maybe I'll write a survey after getting better understanding.

Can somebody help me figure out why my code for Div2C/Div1A fails test case 7?

Code

Fails for following case:

Answer should be 3.

Thank you so much, fixed.

BlueSmoke In the editorial of D2E 2nd paragraph why is this the case when every grid is good then colour would never change please explain.

I think good should be replaced with bad in D2E 2nd paragraph 1st line.

It seems that your are right. Fixed.

Unable to understand Div2-B problem someone please explain.

Look at longest increasing subsequence here: https://cses.fi/book/book.pdf

The idea is the same except you must change the inner loop to match what the problem asks.

My code: https://codeforces.com/contest/1350/submission/79926910.

Can anyone please explain why my DFS solution for Div 2 B problem is giving WA?My submission- 79925444.BlueSmoke or DeadlyCritic Can u please help me debug this? I will be grateful. Thank you.

I checked this out, the bug was your DFS, you didn't run the DFS from all indices, and also you wrote :

What is that else ??, i guess you wanted to say that we don't pop_back $$$x$$$, but what if $$$arr_{temp}$$$ was very small that you should pop_back more numbers.

Here is your code getting AC, barely passing time limits.

I recommend not to use long longs when they are unnecessary, see this, twice faster.

Thank you very much DeadlyCritic for debugging it and providing an explanation. Regarding "else" part, Yes I thought of pop_back in recursion so that I wont need to loop DFS through all indices. Therefore I took only 1 index for DFS.

Also thanks for the suggestion regarding long long. Will keep that in mind. :)

The impact is greatly reduced in G++17 64-bit, see 80071943 and 80071981 (long long). The difference is just 250ms.

Though, I do agree that use long longs only when required.

The solution for Div 2D(1B) fails for the following testcase:

`n = 5`

,`k = 1`

and`a=2,2,0,0,1`

The answer should be

`no`

, but the given solution gives`yes`

.Am I missing something? @BlueSmoke?

[Edit : My bad, the answer should be

`yes`

only]first convert the leftmost 0 to 2 by taking first 3 numbers, then convert the second 0 to 2 too. After 2 conversions you get:-

22001->22201->22221

Now start converting the 2's to 1 by taking one at a time:-

22221->22211->22111->21111->11111

The contest is very interesting,thanks for questions setters.I thinks the ideas of solving B problem is very great,I should make more effort in it.

Thanks for Codeforces, and I hope Codeforces will be better and better. Come on! (Sorry, I'm Chinese. So my English isn't very good.)

can someone explain the time complexity of the seconds solution of DIV2 C ( Orac and LCM problem )

you can find the min prime factor for each number and then divide everyone in O(logV)

I really like the problems <3

Anyone please explain div2.D. I didn't get the editorial explanation.

For Div1A/Div2C, why are we taking

ans=lcm(gcd(d1),gcd(d2)...gcd(dN))?

I am wondering why ans is not

ans=max(gcd(d1),gcd(d2)...gcd(dN))?

Since our requirement is that the ans should divide N-1 integers of a,

could someone please tell why the 2nd answer is wrong?

Hey sanjay_thiru,

I hope you understood the observation that is mentioned in the beginning of the editorial .

Basically you are trying to find the lowest value to divide every distinct set of (n-1) numbers (distinct by index), So each of those values found will appear atleast once in every pair of numbers that we use LCM on.

So in the LCM of all these pair of numbers , those calculated values will exist.

And, since you have to find the GCD of all the pairs , it makes sense to take LCM of all the GCD(di), because we want the greatest value that divides all the LCM of pairs.

Hence the conclusion.

Okay,now it makes sense.

Mathforces!!!!!

Best problem of math

C was easy use this properties

gcd(lcm(a,b),lcm(a,c),lcm(a,d)) = lcm(a,gcd(b,c,d))

How can we prove this formula?

You can see this or this

It can't convince you but for you reference

I am not able to figure out what's wrong with my solution of Div2D (https://codeforces.com/contest/1350/submission/79941143). Can anyone, please help me in coming up with a small test case where my solution will fail.

n =10 ,k = 3,

6 1 6 1 1 1 1 1 1 3

for this test the answer is yes but your code output is no.

For Div2B, I tried simple DFS : 79945578.

Although my code successfully passed, I'm struggling to grasp why this approach is okay.

Anyone can calculate and explain time complexity of this solution?

whats the problem with my code in div2 B. It gave me tle although i used same approach as in editorial. Only difference is that i used memoization while applying dp link to my submission: https://codeforces.com/contest/1350/submission/79881782

Can anyone please check my Div2 C code: https://codeforces.com/contest/1350/submission/79873786

It gave me the memory limit exceeded error. Need help in figuring out the correct answer. Please share the relevant concepts that I didn't use here.

O(N^2) will timeout.

Yes, but it doesn't give time limit exceeded. rather it gives memory exceeded error.

Lol so what? That's still gonna push N^2 in your array which will MLE. It's either gonna MLE or TLE whichever happens first. Maybe just stop forcing solutions to work when it's clearly bad.

In Div1C, why are grid cell's

`(i, j)`

termed as "grid"?I've asked the same question, shouldn't be cell instead of grid?

BlueSmoke can you answer? :)

Will fix it later. Thank you for suggestions. UPD: Done.

Thanks, it makes sense now.

Can anyone can prove this : $$$gcd$$$ $$$of$$$ $$$sequence$$$ $$$lcm(x , ai)$$$ = $$$lcm(x$$$ , $$$gcd$$$ $$$of$$$ $$$sequence$$$ $$$ai$$$) , $$$i = 1, 2, .. n$$$ and $$$x$$$ $$$is$$$ $$$some$$$ $$$integer$$$

Let $$$d=lcm(x,a_i)$$$ and $$$k=gcd(a_1,a_2,...,a_n)$$$， notice that $$$a_i|d$$$,and $$$k|a_i$$$, then $$$k|d$$$.

video editorial for B and C

https://codeforces.com/blog/entry/77306

Thanx, DeadlyCritic for reaching out to each possible comment. Just can't get why my solution is getting TLE which is similar to this one. Can you help?

It overflows in the following line :

because $$$i\cdot i$$$ can be as big as $$$10^{10}$$$.

DeadlyCritic Sir, That's why in my recent submission, I took

`long long`

and not`int`

. Still, it is giving TLE, why?Moreover, since i loops over till sqrt(200006) which is equal to 447 and hence i*i at max equals 200006. I don't see that as a reason. Perhaps something else, which I can't figure out.

Sorry i missed that, for each $$$a_i$$$ you iterate over all primes yes? then it takes $$${n \over log_2n}$$$ time for each $$$a_i$$$(number of primes less than $$$n$$$ is at least $$${n \over log_2n}$$$), then the whole solution works in $$$O(n\cdot {n \over log_2n})$$$ time, which is too much, you don't have to iterate over all primes, just iterate over primes less than 500, then if $$$a_i > 1$$$ then its prime and insert it.

Iv'e tried it and got AC

ok thanx got that. Never thought that it would be a problem doing that !!

Hi, I've been trying to debug my code for Div2 C, I think my code follows the solution #2 described in the editorial, but somehow I keep getting wrong answer on test 4, can anyone please explain where the mistake is?? Thanks in advance My submission

Does your code check big primes? for example the answer to the following test is 199967 :

Oh yeah sorry I didn't think about it.

Thanks

In case anyone is still stuck detail explanation of C and B

MikeMirzayanov Hi, I think offical submission 79869658 by cjy_2021 and submission 79869030 by Bazoka13 are almost the same.It may break the rules of contest, which helps them both become Candidate master.

Hello, I'm very angry about what you said. It's a kind of stigma. My idea of question D is as follows: suppose a interval has a number other than k, then if there is a [i] > = K and a [i + 1] > = k|a [i + 2] > = k, we must be able to yes why? If a [i] a [i + 1] satisfies the condition, then it can make its left or right side expand to the number of > = k until it reaches the value of K. at this time, the number adjacent to K is > = K. according to the meaning of the question (2 + 1) / 2, the interval is assimilated to K no matter how

It's the same way to assimilate a [i] a [i + 2] step by step. He can certainly make [I, I + 2] become a number > = k, because (3 + 1) / 2 = 2 even if a [i + 1] is the smallest in this range, it will not be changed by him. So there are 1.6W participants in this competition. Is there 1.6W talents in each problem solution that can not be judged as cheating? Funny. I believe that everyone who answers the D question correctly uses the same method as me

To sum up, question D is just like the 1 + 1 = 2 you did in primary school. Is there any other solution to 1 + 1 = 2? Is the 1 + 1 = 2 written by primary school students in an examination room cheating? Fuck you

No "Fuck you" needed man.

Can someone tell me whats wrong in this solution 79969659 This is for div 2 C.. giving wrong answer on test case 9.. but when I copy the testcase 9 and run it ..it gives correct answer..How?

Test case 9

2

199999 200000

Let's make Div2D/Div1B a little interesting. If only those subsegments could be transformed whose median is

`k`

, how would you approach the problem?Can someone please explain the difference between $$$\displaystyle E_x$$$ and $$$\displaystyle E_{x}^{'}$$$ in Div1D editorial ?

$$$E_x$$$ is the expected time that the game ends on $$$x$$$, wheras $$$E_x'$$$ is the expected time in a "modified rules" game where we stop

onlywhen all biscuits are on $$$x$$$.Thanks, got it.

Hey, here is my submission of Div1-C https://codeforces.com/problemset/submission/1349/79994169 I used the same logic as in editorial but using DP. It gave WA on 6th testcase and I am not able to figure the error, can anyone help?

How do i optimize the memory for Div 2E, I'm getting a MLE on test 8. 80013481 is my solution.

Thanks. UPD — I'm sorry, I changed the submission ID

You sent a message on wrong thread, this is most recent div1,div2 round.

Your problem is of a old div3.

And you get TLE on TC12, is this the correct submission? [Reason for which BTW is because you don't break if

`flag = false`

so it becomes an infinite loop.]I'm sorry I made a mistake in the Submission ID

i kept on MLEing for problem C, although I had the right idea the whole time (i think I did solution 2). What can you use to calculate the number of pairs besides stuff like maps and arraylists, which MLE? Is there a way to do it in o(1) space? I literally spent 1h30m trying to implement this goddamn problem and I don't think I've ever been more tilted at a codeforces problem than this onehttps://codeforces.com/contest/1350/submission/80029151

Can anyone please explain How exactly to use BFS in Div2E / Div1C ?

What is s.t in the editorial for div 1 Problem 2?

What was the significance of the "unusual memory limits" for Div 1. C? I solved it without really accommodating for it, what solutions was it intending to block?

Sorry for asking this too late , but it will be helpful if someone can explain that what exactly "C" is in the problem div1-D editorial. From what i understood by reading editorial is that it is the expected time for a case where my all items belong to just one person "i" and now i want them to be owned by another person "j". If my interpretation is right then please do tell me how can i calculate this value? Also if this requires some deep knowledge of some concept of probability then please list them so that i can read about them in detail.

I'm not sure, that I understand Div1F1 solution correct. Am I right, that you use '>' and '<' signs between $$$a_i$$$ and $$$a_{i+1}$$$ if $$$a_{i+1}>a_i$$$ and $$$a_{i+1}<a_i$$$ respectively? And what does it mean, that we can combine places next to '<' sign?

What does s or t represent in div1 B editorial? BlueSmoke

In DIV1 C if we apply BFS on every good cell to find shortest time for all bad cells to become good cell, time complexity will be O((nm)**2) right ? Then how it will be a valid solution ? Or is there anything I'm missing such that BFS can be done better than that ?

You are probably thinking in a BFS for every good cell, but it can be done with just one BFS where you initially put all good cells in the queue.

DIV2 D/DIV1 B was brilliant, I understood the proof but it would be great if you could tell how you landed on the statement in the first place.

Easy to understand Tutorial for C intended for beginners.

For the problem 1349A - Orac and LCM we can use the fact that gcd( lcm(A,B) , lcm(A,C) )= lcm( A , gcd(B,C) ).

This is my code and I found it much easier than that in editorial. 115996653

The problem C can be solved in $$$O(n + \log(max(a))$$$ time and $$$O(1)$$$ memory

We have the observation that

So if we define $$$g[x] = \underset{[0, x]}{gcd}(a[i])$$$ and $$$g[0] = 0$$$

Then we have $$$res = \underset{x=1..n}{gcd}(lcm(a_x, a_y) \mid y < x) = \underset{x=1..n}{gcd}(\ lcm[a_x, gcd(a_1, a_2, \dots, a_y)]\ ) = \underset{x=1..n}{gcd}\left( a_x \times \frac{gcd(a_1, a_2, \dots, a_{y-1})}{gcd(a_1, a_2, \dots, a_y)}\right) = \underset{x=1..n}{gcd}\left(a[x] \times \frac{g[x-1]}{g[x]}\right)$$$

Since the $$$gcd$$$ will be reduced for each $$$a_x$$$ we pass, it would only cost $$$O(\log max(a))$$$

independentlySo the total complexity is $$$O(n + \log max(a))$$$ time

For $$$O(1)$$$ space you can solve it onlinely

C++ SolutionDiv1 A 1350A (Orac and LCM)

if the number is odd, the loop will start from n and will decremented till 0 but in very first case i.e i = n, i will give the answer ie n%i == 0 but it is not the smallest divisor.

/////////////////////////////////////

ll f(ll ind,vector v,ll prevind){

if(ind>v.size()) return 0; if(ind==v.size()){ if(ind%prevind==0 && v[prevind]<v[ind]){

}

ll take =0; if(ind%prevind==0 && v[prevind]<v[ind]){ take = f(ind+ind,v,ind); } ll nottake = f(ind*3,v,prevind);

return take+nottake; } void solve(){ ll n; cin>>n;

} int main() { // your code goes here tc{

}

//////////////////////////////////////////////

can anyone help me in solving by this approach Thank You!!

I wanted to say that the n root(n) solution B gives TLE unlike mentioned in the editorial. If anyone got accepted with n root(n) complexity kindly share the submission link. Mine which didn't get accepted ==> 184056612 Thanks.