错装信封

有n个信封,包含n封信,现在把信拿出来,再装回去,要求每封信不能装回它原来的信封,问有多少种装法?

给定一个整数n,请返回装发个数,为了防止溢出,请返回结果Mod 1000000007的值。保证n的大小小于等于300。

测试样例:
2

返回:1

class CombineByMistake {
public:
    int countWays(int n) {
        // write code here
        if(n<=0)
            return -1;
        if(n==1)
            return 0;
        if(n==2)
            return 1;
        int a=0;
        int b=1;
        int res=0;
        for(int i=3;i<=n;i++)
        {
            res=(long)(i-1)*(long)(a+b)%1000000007;
            a=b;
            b=res;
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/zrh_csdn/article/details/80703199