The Review Plan I
64-bit integer IO format: %lld Java class name: Main
Michael takes the Discrete Mathematics course in this semester. Now it's close to the final exam, and he wants to take a complete review of this course.
The whole book he needs to review has N chapter, because of the knowledge system of the course is kinds of discrete as its name, and due to his perfectionism, he wants to arrange exactly N days to take his review, and one chapter by each day.
But at the same time, he has other courses to review and he also has to take time to hang out with his girlfriend or do some other things. So the free time he has in each day is different, he can not finish a big chapter in some particular busy days.
To make his perfect review plan, he needs you to help him.
Input
There are multiple test cases. For each test case:
The first line contains two integers N(1≤N≤50), M(0≤M≤25), N is the number of the days and also the number of the chapters in the book.
Then followed by M lines. Each line contains two integers D(1≤D≤N) and C(1≤C≤N), means at the Dth day he can not finish the review of the Cth chapter.
There is a blank line between every two cases.
Process to the end of input.
Output
One line for each case. The number of the different appropriate plans module 55566677.
Sample Input
4 3 1 2 4 3 2 1 6 5 1 1 2 6 3 5 4 4 3 4
Sample Output
11 284
Source
Author
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 51; 5 const LL mod = 55566677; 6 LL F[maxn] = {1}; 7 void init(){ 8 for(int i = 1; i < maxn; ++i) 9 F[i] = F[i-1]*i%mod; 10 } 11 LL ret = 0; 12 bool vis[maxn][2]; 13 int d[maxn],c[maxn],n,m; 14 void dfs(int cur,int cnt){ 15 if(cur >= m){ 16 if(cnt&1) ret = (ret - F[n - cnt] + mod)%mod; 17 else ret = (ret + F[n - cnt])%mod; 18 return; 19 } 20 dfs(cur+1,cnt); 21 if(!vis[d[cur]][0] && !vis[c[cur]][1]){ 22 vis[d[cur]][0] = vis[c[cur]][1] = true; 23 dfs(cur + 1,cnt + 1); 24 vis[d[cur]][0] = vis[c[cur]][1] = false; 25 } 26 } 27 bool cc[maxn][maxn]; 28 int main(){ 29 init(); 30 while(~scanf("%d%d",&n,&m)){ 31 ret = 0; 32 memset(vis,false,sizeof vis); 33 memset(cc,false,sizeof cc); 34 for(int i = 0; i < m; ++i){ 35 scanf("%d%d",d+i,c+i); 36 if(cc[d[i]][c[i]]){ 37 --i; 38 --m; 39 }else cc[d[i]][c[i]] = true; 40 } 41 dfs(0,0); 42 printf("%lld ",ret); 43 } 44 return 0; 45 }