Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9844 Accepted Submission(s): 4031
1 2 0
1 1
考察错排公式,想了挺久。看了下别人的才有点思路
顺便记一下C(m,n)函数的定义和巩固一下错排公式,以后方便使用
#include<iostream> #include<algorithm> using namespace std; int cmn(int n,int m) { int sum=1; for(int i=1;i<=m;i++) sum=sum*(n--)/i; return sum; } int main(void) { __int64 cuopai[14]={0,0,1}; for (int i=3; i<14; i++)//计算Cmn中n本身个错排的个数 cuopai[i]=(i-1)*(cuopai[i-1]+cuopai[i-2]); __int64 n; while (cin>>n&&n) { if(n==1||n==2) { cout<<1<<endl; continue; } else { __int64 t=(n+1)/2,i; __int64 sum=0; for(i=t; i<=n; i++) { sum+=cuopai[n-i]*cmn(n,n-i);//i为正确个数因此正确的组合数Cmn(i)*错排数cuopai(n-i) } cout<<sum+1<<endl; } } return 0; }