Too Simple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1567 Accepted Submission(s): 510
Problem Description
Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai thought this problem was too simple, sometimes naive. So she ask you for help.
Teacher Mai has m functions f1,f2,⋯,fm:{1,2,⋯,n}→{1,2,⋯,n} (that means for all x∈{1,2,⋯,n},f(x)∈{1,2,⋯,n} ). But Rhason only knows some of these functions, and others are unknown.
She wants to know how many different function series f1,f2,⋯,fm there are that for every i(1≤i≤n) , f1(f2(⋯fm(i)))=i . Two function series f1,f2,⋯,fm and g1,g2,⋯,gm are considered different if and only if there exist i(1≤i≤m),j(1≤j≤n) , fi(j)≠gi(j) .
Teacher Mai has m functions f1,f2,⋯,fm:{1,2,⋯,n}→{1,2,⋯,n} (that means for all x∈{1,2,⋯,n},f(x)∈{1,2,⋯,n} ). But Rhason only knows some of these functions, and others are unknown.
She wants to know how many different function series f1,f2,⋯,fm there are that for every i(1≤i≤n) , f1(f2(⋯fm(i)))=i . Two function series f1,f2,⋯,fm and g1,g2,⋯,gm are considered different if and only if there exist i(1≤i≤m),j(1≤j≤n) , fi(j)≠gi(j) .
Input
For each test case, the first lines contains two numbers
n,m(1≤n,m≤100)
.
The following are m lines. In i -th line, there is one number −1 or n space-separated numbers.
If there is only one number −1 , the function fi is unknown. Otherwise the j -th number in the i -th line means fi(j) .
The following are m lines. In i -th line, there is one number −1 or n space-separated numbers.
If there is only one number −1 , the function fi is unknown. Otherwise the j -th number in the i -th line means fi(j) .
Output
For each test case print the answer modulo
109+7
.
Sample Input
3 3 1 2 3 -1 3 2 1
Sample Output
1HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
#define N 110
LL mod=1000000007;
LL ma[N][N],vis[N];
LL get(LL n,LL m)
{
LL ans=1;
for(LL i=2; i<=n; i++)
ans=ans*i%mod;
LL l=1;
for(LL i=1; i<m; i++)
l=ans*l%mod;
return l;
}
int main()
{
LL n,m;
int flag;
while(~scanf("%lld %lld",&n,&m))
{
LL num=0;
flag=0;
for(LL i=1; i<=m; i++)
{
scanf("%lld",&ma[i][1]);
if(ma[i][1]==-1) num++;
else
{
memset(vis,0,sizeof(vis));
vis[ma[i][1]]=1;
for(LL j=2; j<=n; j++)
{
scanf("%lld",&ma[i][j]);
if(ma[i][j]<1||ma[i][j]>n||vis[ma[i][j]])
flag=1;
vis[ma[i][j]]=1;
}
}
}
if(flag){
printf("0\n");
continue;
}
if(num)
{
LL ans=get(n,num);
printf("%lld\n",ans);
}
else
{
flag=0;
for(LL i=1; i<=n; i++)
{
LL t=i;
for(LL j=m; j>=1; j--)
t=ma[j][t];
if(t!=i)
{
flag=1;
break;
}
}
if(flag) printf("0\n");
else printf("1\n");
}
}
return 0;
}