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(i≤1≤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)

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

Hint

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

题意:

求满足f1(f2(...(fm(i))...))=i的未知的函数有多少种可能。

分析:

答案是(n!)^(m-1)再mod 109+7,m为-1的个数,因为m个不确定的函数,其中的m-1个固定了,那么还有一个也就固定了。每个不确定的都有n!种方案。

如果m为0,则有0种或者1种方案。也就是要看当前的一层一层能否推到f1(f2(...(fm(i))...))=i。

要注意:当某个f里1..n没有全部出现时,即有重复数字时,答案是0。

这题说是too simple,然而好多坑啊!样例只有一组数据,但是实际上可能有多组数据,除此,要注意每次处理新的一组时,哪些变量要清零,还有这题要用long long,n阶乘可以在一开始初始化。

代码:

#include<stdio.h>
#define M 1000000007LL
#define ll long long
#define N 105
#define F(a,b,c) for(int a=b;a<=c;a++)
ll n,m,d,f[N][N],y[N],jc[N]={,},ans;
int main()
{
F(i,,)jc[i]=jc[i-]*i%M;//初始化阶乘
while(~scanf("%lld%lld",&n,&m))
{
d=;ans=;//初始化
F(i,,m)
{
scanf("%lld",&f[i][]);
if(f[i][]==-)d++;
else F(j,,n)
{
scanf("%lld",&f[i][j]);
if(ans)F(k,,j-)
if(f[i][j]==f[i][k])ans=;
}
}
if(ans)
{
if(d==)
{
F(i,,n)y[i]=i;
for(int i=m; i; i--)
F(j,,n)y[j]=f[i][y[j]];//一层层推到f1
F(i,,n&&ans)if(y[i]!=i)ans=;
}
else
F(i,,d-)ans=ans*jc[n]%M;
}
printf("%lld\n",ans);
}
return ;
}

【HDU 5399】Too Simple的更多相关文章

  1. 【HDOJ 5399】Too Simple

    pid=5399">[HDOJ 5399]Too Simple 函数映射问题 给出m函数 里面有0~m个函数未知(-1) 问要求最后1~n分别相应仍映射1~n 有几种函数写法(已给定的 ...

  2. 【hdu 2486】A simple stone game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  3. 【HDU 1757】 A Simple Math Problem

    题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 1 ...

  4. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  5. 一本通1548【例 2】A Simple Problem with Integers

    1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...

  6. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  7. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  8. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  9. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

随机推荐

  1. UESTC 916 方老师的分身III --拓扑排序

    做法: 如果有a<b的关系,则连一条a->b的有向边,连好所有边后,找入度为0的点作为起点,将其赋为最小的价值888,然后其所有能到的端点,价值加1,加入队列,删去上一个点,然后循环往复, ...

  2. Mobile Prototype Dev Res Collection(Unity原型开发资源储备)

    资源储备 本文针对mobile原型开发阶段的资源收集 在做移动端的开发时,当有灵感想做些东西时,若是此时缺少美术资源和可用的脚本,此刻会有些纠结,今天在Assets Store上Mark了一些移动端开 ...

  3. VideoView 播放资源目录raw下的视频

    你把影片copy到res/raw下!檔名小寫加底線,例如:default_video.3gp,在程式碼裡指定uri路徑 String uri = "android.resource://&q ...

  4. python基础随笔

    一: 作用域 对于变量的作用域,只要内存中存在,该变量就可以使用. 二:三元运算 name = 值1 if 条件 else 值2 如果条件为真:result = 值1 如果条件为假:result = ...

  5. D - Palindrome Partitioning (DP)

    Description A palindrome partition is the partitioning of a string such that each separate substring ...

  6. NET Office 组件Spire

    高效而稳定的企业级.NET Office 组件Spire   在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是"这个系统似乎就是专门操作文档的" ...

  7. android Camera 中如何修改缩放变焦参数

    如何修改 zoomRatio   修改过程:   1, 先找到 gZoomRatio 数组序列的值   Location: V:\project_code\project_name\ALPS.JB.M ...

  8. C语言 文件操作6--文件打开方式详解

    fopen文件打开模式 r代表read的简写,+代表可读可写,w代表write,b代表bit二进制位,t代表text r 打开只读文件,该文件必须存在r+ 打开可读可写的文件,该文件必须存在(这里的写 ...

  9. mvc control 请求两次问题

    今天在做项目时,突然发现一个mvc 的control中action被执行了两次,最终发现是由于favicon.ico导致的.问题代码: <link rel="shortcut icon ...

  10. 专门用于微信公众平台的Javascript API

    1 /**! 2 * 微信内置浏览器的Javascript API,功能包括: 3 * 4 * 1.分享到微信朋友圈 5 * 2.分享给微信好友 6 * 3.分享到腾讯微博 7 * 4.新的分享接口, ...