题目链接:传送门
The Review Plan I
Time Limit: 5 Seconds Memory Limit: 65536 KB
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
大意:有n门功课,在n天内完成,有m个条件,例:a b,第a天不能复习第b门功课,问有多少种复习方法。
思路:禁位排序模板题
sum=((sum-pow[n-step])%mod+mod)%mod;
sum=(sum-pow[n-step])%mod;
这两种写法是不同的
相关博客:http://blog.csdn.net/kevin66654/article/details/52629487
举一些小型的例子,手动模拟一下代码,加深理解。
#include<stdio.h>
#include<string.h>
#define mod 55566677
#define N 55
int n,m;
long long sum;
bool xia[N][N],xia1[N],xia2[N];
int a[N],b[N];
long long pow[N];
void start()
{
pow[0]=1;
for(int i=1; i<N; i++)//不用禁位时,i个位置的排列方式
pow[i]=pow[i-1]*i%mod;
}
void dfs(int x,int step)
{
if(x>m)
{
if(step%2)
sum=((sum-pow[n-step])%mod+mod)%mod;
else sum=(sum+pow[n-step])%mod;
return ;
}
dfs(x+1,step);
if(xia1[a[x]]==0&&xia2[b[x]]==0)
{
xia1[a[x]]=xia2[b[x]]=1;
dfs(x+1,step+1);
xia1[a[x]]=xia2[b[x]]=0;
}
}
int main()
{
start();
while(~scanf("%d%d",&n,&m))
{
sum=0;
memset(xia,0,sizeof(xia));
memset(xia1,0,sizeof(xia1));
memset(xia2,0,sizeof(xia2));
for(int i=1; i<=m; i++)
{
scanf("%d%d",&a[i],&b[i]);
if(xia[a[i]][b[i]])//去掉重复的
i--,m--;
else xia[a[i]][b[i]]=1;
}
dfs(1,0);
printf("%lld\n",sum);
}
return 0;
}