Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2068
This problem needs to use the wrong formula and the high school mathematics arrangement combination knowledge.
Permutation combination: "1" arrangement (take m from N and arrange): a_n_m=n!/(n-m)!=n* (n-1) * (n-2) * ... (n-m+1);
"2" combination (take m from N, do not arrange): c_n_m=n!/((n-m)!*m!) =n* (n-1) * (n-2) * ... (n-m+1)/(m* (m-1) * .... 1);
It is therefore not difficult to draw the code for the permutation number:
__<span style= "FONT-SIZE:14PX;" >int64 a_n_m (int n,int m) { __int64 i,up=1; for (i=n;i>=n-m+1;i--) up*=i; return up;} </span>
the code for the number of combinations is:
<span style= "FONT-SIZE:14PX;" >__int64 c_n_m (int n,int m) { __int64 i,up,down;//molecule, denominator up=down=1; for (i=n;i>=n-m+1;i--) up*=i; for (i=m;i>=1;i--) down*=i; return up/down;} </span>
recursive wrong-row formula: N number of rows to be counted as f[n]. The 1th error in N is assumed to be placed in the K position, and there is a n-1 seed-release method. Then the K can be placed in the 1th position, and the rest of the n-2 for the wrong row, for f[n-2]; If the k is not placed in the 1th position, then there are n-1 need the wrong platoon, for F[n-1]. So get the wrong number formula: f[n]= (n-1) * (F[n-1]+f[n-2]), where f[0]=0.f[1]=0,f[2]=1.
for (i=3; i<=25; i++) f[i]= (i-1) * (F[i-1]+f[i-2]);
In the subject "Girls only ask him to correct half or more of the clearance ", so it should be the wrong row 0, 1, 2 to N/2, and then multiply with the number of permutations can be accumulated. Note that you should choose the combination number formula instead of the permutation number formula. See the code below, if you do not understand can reply ~ ~
#include <cstdio> #include <iostream> #include <sstream> #include < cstdlib> #include <cstring> #include <string> #include <climits> #include <cmath> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <set> #include <map>using namespace Std;__int64 c_n_m (int n,int m) {__int64 i,up,down;//numerator, denominator up=down=1; for (i=n;i>=n-m+1;i--) up*=i; for (i=m;i>=1;i--) down*=i; return up/down;} int main () {int n; int i; __int64 f[30]; f[0]=0; f[1]=0; F[2]=1; __int64 sum; for (i=3; i<=25; i++) f[i]= (i-1) * (F[i-1]+f[i-2]); while (~SCANF ("%d", &n)) {sum=0; if (n==0) break; else {for (i=0; i<=n/2; i++) sum+=f[i]*c_n_m (n,i); } printf ("%lld\n", sum+1), and//f[0]=1 when all the answers are correct, so add 1. }}<span style= "FONT-SIZE:17PX;" ></span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2068 RPG's Wrong row (permutation, staggered) very detailed ~