HUD-5399 Too Simple(数学)
2015-08-18 19:20
281 查看
Too Simple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)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 10^9+7.
Sample Input
3 3 1 2 3 -1 3 2 1
Sample Output
1 HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
思路与官方的一样:
首先要求每个f_ifi是个排列,否则如果某个f_ifi将两个数映射向同一个数,那么最后这两个数得到的值一定相同。
如果还剩一个位置为-1−1,那么这个排列是唯一确定的,假设X*f_i*Y=IX∗fi∗Y=I,那么f_i=X^{-1}*Y^{-1}fi=X−1∗Y−1.
所以假设有c(c\geq
1)c(c≥1)个-1−1,那么答案为(n!)^{c-1}(n!)c−1个可行的方案。
注意特判所有函数都已知的情况。
#include <cstdio> #include <cstring> using namespace std; const long long MOD=1000000007; long long f[101]; int a[101][101]; bool vis[101]; void Init() { f[0]=f[1]=1; for(long long i=2;i<=100;++i) f[i]=(f[i-1]*i)%MOD; } long long quickpow(long long m,long long n) { long long b=1; while(n>0){ if (n&1) b=(b*m)%MOD; n=n>>1; m=(m*m)%MOD; } return b; } int n,m; bool Judge() {//特判所有函数已知的情形 int i,j,x; for(i=1;i<=n;++i) { x=i; for(j=m-1;j>=0;--j) x=a[j][x]; if(x!=i) return false; } return true; } int main() { int i,j,cnt; bool ok; Init(); while(2==scanf("%d%d",&n,&m)) { cnt=0,ok=true; for(i=0;i<m;++i) { scanf("%d",&a[i][1]); if(a[i][1]==-1) ++cnt; else { memset(vis,false,sizeof(vis)); vis[a[i][1]]=true; for(j=2;j<=n;++j) { scanf("%d",&a[i][j]); if(vis[a[i][j]])//刚开始以为是单射函数,结果WA了好久。如果存在非单射函数,则结果为0 ok=false; else vis[a[i][j]]=true; } } } if(cnt==0) printf("%d\n",Judge()?1:0); else printf("%I64d\n",ok?quickpow(f ,cnt-1):0); } return 0; }
相关文章推荐
- HDU 1865 六度分离 <裸的迪杰斯特拉算法>
- springMVC 配置jdbcTemplate连接Oracle数据库出错
- Bag Problem
- hadoop native本地库问题总结
- 初遇 dotcloud
- Bag Problem
- Children’s Queue 1297 (大数)
- iOS开发中的神技能
- 迈出从3K到1W的重要一步——掌握设计模式
- Eclipse Outline视图你所不知道的icon含义
- thinkphp 上传单张图片
- hdu 3555数位DP 入门题
- hdu2647(拓扑排序)
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- iOS屏幕适配方案-Auto Layout
- erlang模块的导入和导出
- Android Studio文件分组插件
- 1096. Consecutive Factors (20)
- vs2015小要记
- 最大子段和||最大子矩阵和||最大全1子矩阵||最大全1子正方形||