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. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  2. Android优化—— 内存分析工具 MAT 的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,其中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...

  3. f2fs解析(十)nid 如何从nat_root中删除

    上面我们谈到了一个nid如何从free_nid中转移到node_info中去[分别有一个链表和一棵基数树搭伙做事],讲free_nid时,详细说明了free_nid中是如何进如何出的,上一篇说了nid ...

  4. C++ map的使用

    C++ map的基本操作和使用 来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博 ...

  5. [iOS翻译]《iOS7 by Tutorials》系列:iOS7的设计精髓(下)

    我们继续上篇的内容 四.聚焦于内容 在iOS7里,强调的不是眼花缭乱的装饰效果,而是最重要的内容本身. 下面我们来探讨这个主题: 1.删除不必要的内容 伟大的设计更多是减法和加法的组合. 虽然很酷的想 ...

  6. python数字图像处理(4):图像数据类型及颜色空间转换

    一.图像数据类型及转换 在skimage中,一张图片就是一个简单的numpy数组,数组的数据类型有很多种,相互之间也可以转换.这些数据类型及取值范围如下表所示: Data type Range uin ...

  7. C中的预编译宏定义

     可以用宏判断是否为ARC环境 #if _has_feature(objc_arc) #else //MRC #endif C中的预编译宏定义 -- 作者: infobillows 来源:网络 在将一 ...

  8. 实践2.4 ELF文件格式分析

    实践2.4 ELF文件格式分析 1.ELF文件头 查看/usr/include/elf.h文件: #define EI_NIDENT (16) typedef struct { unsigned ch ...

  9. polya计数定理在ACM-icpc中的应用

    [数学公式] PG(x1,x2,...,xn) = 1/|G| * ∑π∈G x1^b1 * x2^b2*...*bn^bn   其中π是1^b12^b2...n^bn型轮换 然后一般染色情况下x1= ...

  10. 数据库防火墙如何防范SQL注入行为

    SQL注入是当前针对数据库安全进行外部攻击的一种常见手段.现有主流应用大多基于B/S架构开发,SQL注入的攻击方式正是利用web层和通讯层的缺陷对数据库进行外部恶意攻击.将SQL命令巧妙的插入通讯的交 ...