Too Simple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1673 Accepted Submission(s): 547
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 functionsf1,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
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. Ini -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
If there is only one number
Output
For each test case print the answer modulo
109+7
.
Sample Input
3 3
1 2 3
-1
3 2 1
Sample Output
1
HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
Author
xudyh
Source
题目大意:
这个题目的意思是很重要的,如果一旦读错了那这个题目就可以宣布不用做啦,刚开始我们就是读错题了,所以就…,后来通过上网看一下题解结果发现题目读错了。。。
题目的意思是:现在有
m
个
i∈{1,2,...,n},f1(f2(...(fm(i)))=i 的方案总数。
解题思路;
其实这个题目经过观察之后就会发现,基本上方案数只与 −1 的个数有关系,因为当 m−1 个方程确定完之后,剩下的一个方程肯定就是确定的了,
也就是说假设 −1 的个数是 cnt 那么答案就是 (N!)cnt−1 ,然后特判一下,因为这个函数必须是 1−n 的,如果不是这样的就是直接输出 0 ,
当
/**
2016 - 09 - 01 晚上
Author: ITAK
Motto:
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1e9+5;
const int MAXN = 1e2+5;
const LL MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
LL res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Out(LL a)///输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
LL fac[MAXN];
void Init()
{
fac[0] = 1;
fac[1] = 1;
for(LL i=2; i<MAXN; i++)
fac[i] = (fac[i-1]*i)%MOD;
}
LL quick_mod(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b & 1)
ans = (ans*a)%MOD;
b>>=1;
a = (a*a)%MOD;
}
return ans;
}
int a[MAXN][MAXN];
int vis[MAXN], ans[MAXN];
int main()
{
Init();
int n, m;
while(cin>>n>>m)
{
LL cnt = 0;
for(int i=1; i<=m; i++)
{
cin>>a[i][1];
if(a[i][1] == -1)
{
cnt++;
continue;
}
for(int j=2; j<=n; j++)
cin>>a[i][j];
}
for(int i=1; i<=m; i++)
{
memset(vis, 0, sizeof(vis));
if(a[i][1] == -1)
continue;
for(int j=1; j<=n; j++)
vis[a[i][j]] = 1;
for(int i=1; i<=n; i++)
if(!vis[i])
{
puts("0");
goto endW;
}
}
if(cnt == 0)///没有 -1 的时候
{
for(int i=1; i<=n; i++)
ans[i] = i;
for(int i=m; i>0; i--)
for(int j=1; j<=n; j++)
ans[j] = a[i][ans[j]];
for(int i=1; i<=n; i++)
{
if(ans[i] != i)
{
puts("0");
goto endW;
}
}
puts("1");
}
else
{
LL ret = fac[n];
ret = quick_mod(ret, cnt-1);
cout<<ret<<endl;
}
endW:;
}
return 0;
}