hdu 4535 吉哥系列故事——礼尚往来
http://acm.hdu.edu.cn/showproblem.php?pid=4535
错排公式:a[i]=(i-1)*(a[i-2]+a[i-1]);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 #define maxn 10000 6 using namespace std; 7 const ll mod=1000000000+7; 8 9 int t; 10 int n; 11 ll a[maxn]; 12 13 int main() 14 { 15 scanf("%d",&t); 16 a[1]=0; a[2]=1; 17 for(int i=3; i<=100; i++) 18 { 19 a[i]=((i-1)*(a[i-2]+a[i-1]))%mod; 20 } 21 while(t--) 22 { 23 scanf("%d",&n); 24 printf("%lld\n",a[n]); 25 } 26 return 0; 27 }