Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=5399;
Test instructions
Give you m function f1,f2,?, fm:{1,2,?, n}→{1,2,?, N} (that is, all x∈{1,2,?, n}, corresponding f (x) ∈{1,2,?, n}), known as a part of the function value, ask you how many different combinations make all I (1≤i≤n) , Meet F1 (F2 (FM (i))) =i
For the set of functions F1,F2,?, FM and G1,G2,?, GM, when and only if there is an I (1≤i≤m), J (1≤j≤n), Fi (J) ≠gi (j), such a combination is considered different. Where input-1 means that f (x) can be equal to any one value;
Analysis:
In fact, if you think about it, you will find that the solution of this problem is related to the number of 1, when there is only one-1, because the corresponding relationship has been decided, so there are only 1 feasible solutions, and when you have two-1, one of the functions can be determined according to the change of another function, so there are n! species. And so on, when there is a K-1, (n!) ^ (k-1);
Of course, when the value of K is 0, there is a situation where the output 1 is to be made out of Dfs;
The code is as follows:
#include <iostream> #include <map> #include <cmath> #include <string.h> #include <cstdio> using namespace Std;const int Mod=1e9+7;long long F[150];long long quick_mod (long long A,long long B) {long long ans=1; while (b) {if (b&1) Ans=ans*a%mod; A=a*a%mod; b>>=1; } return ans; Fast Power int A[105][105];bool vis[105];int DFS (int t,int x) {if (t==1) return a[t][x]; Return Dfs (T-1,a[t][x]);} The DFS Solver judges int main () {int n,m; F[1]=1; for (int i=2;i<150;i++) F[i]=f[i-1]*i%mod; while (scanf ("%d%d", &n,&m)!=eof) {int k=0; BOOL Flag=false; for (int i=1;i<=m;) {int x; memset (vis,0,sizeof (VIS)); for (int j=1;j<=n;j++) {scanf ("%d", &x); if (x==-1) {k++; Goto Next; } a[i][j]=x; Vis[x]=true; } for (int j=1;j<=n;j++) if (vis[j]==false) flag=true; next:i++; } if (flag==true) {cout<<0<<endl; } else if (k!=0) {cout<<quick_mod (f[n],k-1) <<endl; } else{int i; for (i=1;i<=n;i++) if (Dfs (m,i)!=i) break; if (i>n) puts ("1"); Else puts ("0"); }} return 0;}
Hdu 5399 (mathematical reasoning)