您的位置:首页 > 大数据 > 人工智能

HDU 5399 Too Simple(数学 + 找规律)——2015 Multi-University Training Contest 9

2016-09-01 18:59 375 查看
传送门

Too Simple

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1673 Accepted Submission(s): 547


[align=left]Problem Description[/align] 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).

[align=left]Input[/align] 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).

[align=left]Output[/align] For each test case print the answer modulo 109+7.

[align=left]Sample Input[/align]
3 3

1 2 3

-1

3 2 1

[align=left]Sample Output[/align]
1

HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions. 

[align=left]Author[/align] xudyh
[align=left]Source[/align]

题目大意:

这个题目的意思是很重要的,如果一旦读错了那这个题目就可以宣布不用做啦,刚开始我们就是读错题了,所以就…,后来通过上网看一下题解结果发现题目读错了。。。

题目的意思是:现在有 m 个 {1,2...n} → {1,2...,n} 的映射,其中有的映射是已经知道的,还有一些是未知的记作 −1 ,求对任意

i∈{1,2,...,n},f1(f2(...(fm(i)))=i 的方案总数。

解题思路;

其实这个题目经过观察之后就会发现,基本上方案数只与 −1 的个数有关系,因为当 m−1 个方程确定完之后,剩下的一个方程肯定就是确定的了,

也就是说假设 −1 的个数是 cnt 那么答案就是 (N!)cnt−1,然后特判一下,因为这个函数必须是 1−n 的,如果不是这样的就是直接输出 0,

当 cnt = 0 的时候需要特判一下,f1(f2(...(fm(i)))=i,从内到外一次判断一下就行了。

/**
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
;
ret = quick_mod(ret, cnt-1);
cout<<ret<<endl;
}
endW:;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐