HDU 2068(错排+组合数)

本体要用long long

/**
 * hdu2068
 * 错排+组合数
 */

#include
#include

using namespace std;

const int mmax = 30;
typedef long long ll;

//巧妙求组合数
ll C(int m, int n)
{
    ll a = 1;
    ll b = 1;

    for(int i = 1; i <= m; i++, n--)
    {
        a *= n;
        b *= i;
    }

    return a/b;
}

int main()
{
    int w[mmax];
    w[0] = 1; w[1] = 0;
    for(int i = 2; i <= 25; i++)
    {
        w[i] = (i-1)*(w[i-1] + w[i-2]);
    }

    int n;
    while(scanf("%d", &n) && n)
    {
        ll s = 0;
        for(int i = 0; i <= n/2; i++)
        {
            s+= w[i] * C(i, n);
        }
        printf("%lld\n", s);
    }


    return 0;
}

你可能感兴趣的:(---水---)