ZOJ 3687 The Review Plan I

这篇博客介绍了一个利用容斥原理和搜索算法解决课程复习计划的问题。通过多步骤的解析,作者展示了如何在有限的时间内合理安排不同章节的复习顺序,以最大化复习效率。此外,文章还提供了代码实现细节,帮助读者理解并应用到实际情境中。

The Review Plan I

Time Limit: 5000ms
Memory Limit: 65536KB
This problem will be judged on  ZJU. Original ID: 3687
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≤DN) and C(1≤CN), 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

LI, Huang
 
解题:容斥原理+搜索
 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\n",ret);
43     }
44     return 0;
45 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4819886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值