hdu5399 Too simple
2015-08-18 20:25
323 查看
*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 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).
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.
思路:若不存在-1,则要判断是否满足函数的对应关系,如果满足输出1,不满足输出0. 若存在-1 ,个数为 ans 分析得结果为 (n!)^(ans-1)。
代码
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).
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.
思路:若不存在-1,则要判断是否满足函数的对应关系,如果满足输出1,不满足输出0. 若存在-1 ,个数为 ans 分析得结果为 (n!)^(ans-1)。
代码
#include<iostream> #include<cstdio> #define ll __int64 #define mod 1000000007 using namespace std; ll ff[105]; int main() { ff[1]=1; for(int i=2;i<=101;i++) ff[i]=ff[i-1]*i%mod;// 计算n! int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int f[105][105]; int ans=0;ll res=1; for(int i=1;i<=m;i++) { scanf("%d",&f[i][1]); if(f[i][1]==-1) ans++; else{ for(int j=2;j<=n;j++) { scanf("%d",&f[i][j]); for (int k = j - 1; k; k--) if (f[i][j] == f[i][k]) res = 0; //判断有没有函数对应关系是否为一一对应,不是则结果为0 } } } // 没有-1时,判断i(1≤i≤n),f1(f2(⋯fm(i)))=i是否成立 if(ans==0) { int flag=1; for(int j=1;j<=n;j++) { int a=f[m][j]; for(int i=m-1;i>=1;i--) a=f[i][a]; if(a!=j)flag=0; if(!flag) break; } if(!flag) {printf("0\n");continue;} if(flag) {printf("1\n") ;continue;} } else{ for(int i=1;i<=ans-1;i++) res=(res*ff )%mod; } printf("%I64d\n",res); } return 0; }
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- PHP模拟asp.net的StringBuilder类实现方法
- javascript用层模拟可移动的小窗口
- 自编jQuery插件实现模拟alert和confirm
- PHP模拟asp中response类实现方法
- javascript 模拟点击广告
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- php实现模拟post请求用法实例
- JavaScript实现MIPS乘法模拟的方法
- 模拟xcopy的函数
- php模拟服务器实现autoindex效果的方法
- C# SendInput 模拟鼠标操作的实现方法
- PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
- php模拟登陆的实现方法分析
- php模拟用户自动在qq空间发表文章的方法
- php 模拟 asp.net webFrom 按钮提交事件实例
- php模拟post提交数据的方法
- JavaScript模拟重力状态下抛物运动的方法
- JavaScript模拟实现键盘打字效果