Problem Description
You are given a permutation a from 0 to n1 and a permutation b from 0 to m1.

Define that the domain of function f is the set of integers from 0 to n1, and the range of it is the set of integers from 0 to m1.

Please calculate the quantity of different functions f satisfying that f(i)=bf(ai) for each i from 0 to n1.

Two functions are different if and only if there exists at least one integer from 0 to n1 mapped into different integers in these two functions.

The answer may be too large, so please output it in modulo 109+7.
 

 

Input
The input contains multiple test cases.

For each case:

The first line contains two numbers n, m(1n100000,1m100000)

The second line contains n numbers, ranged from 0 to n1, the i-th number of which represents ai1.

The third line contains m numbers, ranged from 0 to m1, the i-th number of which represents bi1.

It is guaranteed that n106, m106.
 

 

Output
For each test case, output "Case #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 

 

Sample Input
3 2
1 0 2
0 1
3 4
2 0 1
0 2 3 1
 

 

Sample Output
Case #1: 4
Case #2: 4
 
启发博客:http://blog.csdn.net/changjiale110/article/details/76448385
以下题解摘自此博客

有两个,数组a是[0~n-1]的排列,数组b是[0~m-1]的排列。现在定义f(i)=b[f(a[i])]; 
问f(i)有多少种取值,使得表达式f(i)=b[f(a[i])]全部合法。

寻找都推关系,根据已知的a数组和b数组,找出a,b两个数组于f的对应关系。

因为提上给出的f(i)=b[f(a[i])],此时我们可以转换一下,将a[i]看作要求的参数,则上面的式子可以转 换为f(i)=b[f(a[i])]=b[b[f(a[a[i]])]],如果我们已知这样转换下去,直到将最后的结果能够转换成f(i)为止 的话。可发现,最里面的一成是i->a[i]->a[a[i]]···-.i的一个环,这个环的循环可以决定f()函数的循环, 进而决定外面的b的循环,相当于f的循环可以由a和b的环来决定。 
以第一个样例 a={1,0,2} b={0,1}为例: 
那么f(0)=b[f(1)] f(1)=b[f(0)] f(2)=b[f(2)] 
这里有两个环分别为 f(0)->f(1) 和f(2) 
所以我们的任务就是在b中找环,该环的长度必须为a中环的长度的约数。 
为什么必须的是约数呢? 
因为如果b的环的长度是a的环的长度的约数的话,那也就意味着用b这个环也能构成a这个环,只不 过是多循环了几次而已。 
然后找到a中所有环的方案数,累乘便是答案。 
为什么要累乘呢?我最开始一直以为要累加。 
这个就用到了排列组合的思想,因为肯定要f(i)肯定要满足所有的数,而a中的每个环都相当于从a中 取出几个数的方案数,所以总共的方案数应该累乘。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 int n,m;
10 const int maxn=100005;
11 const int mod=1e9+7;
12 int a[maxn],b[maxn],lenb[maxn];
13 //lenb[i]记录的是b中循环长度为i的环个数
14 vector<int>aa;//构建环
15 vector<int>fac[maxn];//记录因子
16 bool vis[maxn];
17 //找环,并返回环的大小
18 int dfs(int i,int*c)
19 {
20     if(vis[i])
21         return 0;
22     vis[i]=1;
23     return dfs(c[i],c)+1;
24 }
25 
26 void get_fac()
27 {
28     for(int i=1;i<=100000;i++)
29         for(int j=i;j<=100000;j+=i)
30             fac[j].push_back(i);
31     //fac[j]里面保存的是长度为j的环的因子
32 }
33 
34 int main()
35 {
36     int T=1;
37     get_fac();
38     while(~scanf("%d%d",&n,&m))
39     {
40         for(int i=0;i<n;i++)
41             scanf("%d",&a[i]);
42         for(int i=0;i<m;i++)
43             scanf("%d",&b[i]);
44         aa.clear();
45         memset(vis,false,sizeof(vis));
46         for(int i=0;i<n;i++)
47         {
48             if(vis[i])continue;
49             aa.push_back(dfs(i,a));//aa数组中存下a中每个环的长度
50         }
51         memset(vis,false,sizeof(vis));
52         memset(lenb,0,sizeof(lenb));
53         for(int i=0;i<m;i++)
54         {
55             if(vis[i])
56                 continue;
57             lenb[dfs(i,b)]++;
58         }
59         long long ans=1;
60         int L=aa.size();
61         //根据a的每个环去找b约数环
62         for(int i=0;i<L;i++)
63         {
64             int lena=aa[i],ll=fac[lena].size();
65             long long res=0;
66             for(int j=0;j<ll;j++)
67             {
68                 int lb=fac[lena][j];//lb是长度为lena的环的一个因子
69                 res=(res+(long long)lb*lenb[lb])%mod;//乘上长度为这个因子的环的个数
70             }
71             ans=(ans*res)%mod;
72         }
73         printf("Case #%d: %lld\n",T++,ans);
74     }
75     return 0;
76 }