歪解:爆空间的DP
//Writer:jr HSZ;%%%WJMZBMR
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define reg register int
#define f(i,a,b) for(reg i=a;i<=b;i++)
using namespace std;
int n,sum;
int a[6];
LL f[31][31][31][31][31];
int main() {
while(~scanf("%d",&n)) {
sum=0;
if(!n)return 0;
memset(f,0,sizeof f);
memset(a,0,sizeof a);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
f[0][0][0][0][0]=1;
for(int a5=0; a5<=a[5]; a5++) {
for(int a4=0; a4<=a[4]; a4++) {
for(int a3=0; a3<=a[3]; a3++) {
for(int a2=0; a2<=a[2]; a2++) {
for(int a1=0; a1<=a[1]; a1++) {
if(a1 < a[1])
f[a1+1][a2][a3][a4][a5] += f[a1][a2][a3][a4][a5];
if((a2 < a1) &&(a2 < a[2]))
f[a1][a2+1][a3][a4][a5] += f[a1][a2][a3][a4][a5];
if((a3 < a2) && (a3 < a1) && (a3<a[3]))
f[a1][a2][a3+1][a4][a5] += f[a1][a2][a3][a4][a5];
if((a4 < a3) && (a4 < a2) && (a4 < a1) && (a4<a[4]))
f[a1][a2][a3][a4+1][a5] += f[a1][a2][a3][a4][a5];
if((a5 < a4) && (a5 < a3) && (a5 < a2) && (a5 < a1) && (a5<a[5]))
f[a1][a2][a3][a4][a5+1] += f[a1][a2][a3][a4][a5];
}
}
}
}
}
cout << f[a[1]][a[2]][a[3]][a[4]][a[5]] << endl;
}
正解:杨氏矩阵+勾长公式
证明:https://blog.csdn.net/acdreamers/article/details/14549077
(让我证明?不存在的)
#include <cstdio>
#include <cstring>
long long n,c[10],a[10][55],sum[55*55<<1],cnt;
long long gcd(unsigned long long a,unsigned long long b) {
if(!b) return a;
return gcd(b,a%b);
}
int main() {
while(~scanf("%lld",&n)&&n) {
long long cnt=0,x=1,y=1;
std::memset(sum,0,sizeof sum);
for(int i=1; i<=n; i++) scanf("%lld",&c[i]);
for(int i=1; i<=n; i++)
for(int j=1; j<=c[i]; j++) {
cnt++;
for(int k=i+1; k<=n; k++) {
if(c[k]>=j) sum[cnt]++;
else break;
}
sum[cnt]+=c[i]-j+1;
}
long long tp;
for(int i=1; i<=cnt; i++) {
x*=i;
y*=sum[i];
tp=gcd(x,y);
x/=tp,y/=tp;
}
printf("%lld\n",x/y);
}
}
[POJ2279] Mr.Young's Picture Permutations
原文地址:https://www.cnblogs.com/sdfzhsz/p/9278798.html
时间: 2024-10-30 16:22:39