Help to Understand A solution to 1515E Phoenix and Computers

Revision en3, by hello99acres, 2021-09-11 16:31:38

Hello Codeforces, I was trying to understand this problem 1515E - Феникс и компьютеры , I've read the editorial and understood it. But then I found this solution :114947787 by Marcus0510 which I've copied here for convenience:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,b,a) for(i=b;i>=a;i--)
#define ll long long
#define db double
using namespace std;

const int maxn=505;

ll f[maxn][maxn],ans;
int i,j,k,n,md;

int main(){
	scanf("%d%d",&n,&md);
	f[1][1]=1;
	fo(i,1,n){
		fo(j,1,i){
			f[i+1][j]=(f[i+1][j]+f[i][j]*j*2)%md;
			f[i+2][j+1]=(f[i+2][j+1]+f[i][j]*(j+1))%md;
			if (i==n) ans=(ans+f[i][j])%md;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

which is much smaller than the editorial and I wasn't able to understand what the author has done here, except that it uses D.P. and the variable 'i' represents if there were i computers in the question and thus we do a sum over all j from 1 to n where i = n in the end. Please help me understand what's being done here and what is the significance of the variable j?

Tags #combinatorics, #dp

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English hello99acres 2021-09-11 16:31:38 5 Tiny change: ' the end. So please help' -> ' the end. Please help'
en2 English hello99acres 2021-09-11 16:30:27 31
en1 English hello99acres 2021-09-11 16:29:12 1298 Initial revision (published)