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. AC日记——传话 codevs 1506 (tarjan求环)

    1506 传话  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 一个朋友网络,如果a认识b,那么如果a第 ...

  2. readLine读取socket流的时候产生了阻塞

    BufferedReader的readLine方法,只要读到流结束或者流关闭,就会返回null 在读取文件的时候,文件结尾就是流的结尾,但对于Socket而言不是的.不能认为流中数据读完了就是流的结尾 ...

  3. 05JavaIO详解_仿照IO源码自己去实现一个IO流(为了加深印象,本身没有价值)

    版本会越来越难: 版本1:只写一个read方法 package com.guigu.shen.InputStream; import java.io.IOException; import java. ...

  4. Gitub

    1.下载地址(注册:jackchn,jackchn@foxmail.com) http://windows.github.com/ 2.使用 github for Windows使用介绍 搭建一个免费 ...

  5. 公钥(Public Key)与私钥(Private Key)

    公钥(Public Key)与私钥(Private Key)是通过一种算法得到的一个密钥对(即一个公钥和一个私钥),公钥是密钥对中公开的部分,私钥则是非公开的部分.公钥通常用于加密会话密钥.验证数字签 ...

  6. char 转wchar_t 及wchar_t转char

    利用WideCharToMultiByte函数来转换,该函数映射一个unicode字符串到一个多字节字符串.通常适合于window平台上使用. #include <tchar.h> #in ...

  7. Table AdvanceTable针对表内行禁用multipleSelection , singleSelection

    OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); table.setInserta ...

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

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

  9. [iOS翻译]Cocoa编码规范

        简介: 本文整理自Apple文档<Coding Guidelines for Cocoa>.这份文档原意是给Cocoa框架.插件及公共API开发者提供一些编码指导,实质上相当于Ap ...

  10. chrome http Request Header 修改插件

    chrome http Request Header 修改插件 2013-05-31 11:03:03|  分类: JavaScript |  标签:chrome  extensions  chang ...