标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17475 Accepted Submission(s): 7284
错排公式:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define mem(x,y) memset(x,y,sizef(x)) 7 using namespace std; 8 typedef long long LL; 9 const int INF=0x3f3f3f3f; 10 11 int main(){ 12 int n; 13 LL ans[21]; 14 ans[1]=0;ans[2]=1; 15 for(int i=3;i<=20;i++) 16 ans[i]=(i-1)*(ans[i-1]+ans[i-2]); 17 while(~scanf("%d",&n))printf("%I64d\n",ans[n]); 18 return 0; 19 }
容斥代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define mem(x,y) memset(x,y,sizef(x)) 7 using namespace std; 8 typedef long long LL; 9 const int INF=0x3f3f3f3f; 10 LL fac[21]; 11 int main(){ 12 int n; 13 fac[0]=1; 14 for(int i=1;i<=20;i++)fac[i]=fac[i-1]*i; 15 while(~scanf("%d",&n)){ 16 LL ans=0; 17 for(int i=0;i<=n;i++){ 18 if(i&1)ans-=fac[n]/fac[i]; 19 else ans+=fac[n]/fac[i]; 20 } 21 printf("%I64d\n",ans); 22 } 23 return 0; 24 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4944733.html