标签:etc iostream turn 分发 while style std space tchar
此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。
题目链接:http://oj.noi.cn/oj/#main/show/1179
分析:
错排问题。公式:f[n] = (n-1)*(f[n-1]+f[n-2])
推导(复制自百度百科):
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 using namespace std; 7 8 inline void read(int &x) 9 { 10 x = 0;char ch = getchar(), c = ch; 11 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 12 while(ch <= ‘9‘ && ch >= ‘0‘)x = (x<<1)+(x<<3)+ch - ‘0‘, ch = getchar(); 13 if(c == ‘-‘)x = -x; 14 } 15 16 long long f[30]; 17 18 int main() 19 { 20 int n; 21 read(n); 22 f[1] = 0,f[2] = 1; 23 for(int i = 3;i <= n;++ i) 24 f[i] = (i-1)*(f[i-1]+f[i-2]); 25 printf("%lld\n",f[n]); 26 return 0; 27 }
标签:etc iostream turn 分发 while style std space tchar
原文地址:http://www.cnblogs.com/shingen/p/7501333.html