hdu 2068 (Staggered + composition) water title

hdu 2068 (Staggered + composition) water title

Topic Link

Done once, she did not do it before, this time to do so, the feeling is quite water, plus the combination formula is wrong row on it

错排 official:f[n]=(n-1)*(f[n-1]+f[n-2]);

Combination formula:

c[0]=1;
/**组合公式(递推实现)*/
for(int i=1;i<=n;i++){
	c[i]=c[i-1]*(n-i+1)/i;}

Since the topic that long answer to half or more, and then only to the requirement:

Then the following code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
ll f[30];
ll c[30];
int main(){
    f[0]=1;
    f[1]=0;
    f[2]=1;
    /**全错排公式*/
    for(int i=3;i<=25;i++){
        f[i]=(f[i-1]+f[i-2])*(i-1);
    }
    int n;
    while(~scanf("%d",&n)){
        if(n==0)
            break;
        memset(c,0,sizeof(c));
        c[0]=1;
        /**组合公式(递推实现)*/
        for(int i=1;i<=n;i++){
            c[i]=c[i-1]*(n-i+1)/i;
        }
        ll sum=0;
        int m;
        for(int i=0;i<=n/2;i++){
            sum+=c[n-i]*f[i];
        }
        printf("%lld\n",sum);
    }
    return 0;
}

Hdu 2049 which is also a problem related to the wrong row. . . .

Topic Link

Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/102643792