3 3 1 2 3 -1 3 2 1
1HintThe order in the function series is determined. What she can do is to assign the values to the unknown functions.
已知函数有一个不是双射,必定无解
若所有函数均已知,直接判断
否则,只要有一个 -1, 那么必能构造出解
有k个-1,前k-1个随便排 ,最后那个就能推出 ans=(n!)^(k-1)
#include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (1000000007) #define MAXN (100+10) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} int n,m; bool b[MAXN]; int f[MAXN][MAXN]; int g[MAXN]; int main() { // freopen("D.in","r",stdin); while(cin>>n>>m) { int cnt=0; bool flag=0; For(i,m) { MEM(b) int p; scanf("%d",&p); if (p==-1) { ++cnt;continue;} else f[i][1]=p; b[p]=1; Fork(j,2,n) { scanf("%d",&p);f[i][j]=p; if (!b[p]) b[p]=1; else flag=1; } } if (flag) { puts("0");continue; } if (!cnt) { For(i,n) g[i]=i; ForD(i,m) For(j,n) g[j]=f[i][g[j]]; bool flag=0; For(i,n) if (g[i]^i) flag=1; if (flag) puts("0");else puts("1"); continue; } if (cnt==1) { puts("1"); continue; } ll ans=1,p2=1; For(i,n) p2=mul(p2,i); For(i,cnt-1) ans=mul(p2,ans); cout<<ans<<endl; } return 0; }