Problem Description
You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1.

Define that the domain of function f is the set of integers from 0 to n−1, and the range of it is the set of integers from 0 to m−1.

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

Two functions are different if and only if there exists at least one integer from 0 to n−1 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. (1≤n≤100000,1≤m≤100000)

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

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

It is guaranteed that ∑n≤106, ∑m≤106.

 
Output
For each test case, output "Case #x: y" 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中 取出几个数的方案数,所以总共的方案数应该累乘。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int n,m;
const int maxn=;
const int mod=1e9+;
int a[maxn],b[maxn],lenb[maxn];
//lenb[i]记录的是b中循环长度为i的环个数
vector<int>aa;//构建环
vector<int>fac[maxn];//记录因子
bool vis[maxn];
//找环,并返回环的大小
int dfs(int i,int*c)
{
if(vis[i])
return ;
vis[i]=;
return dfs(c[i],c)+;
} void get_fac()
{
for(int i=;i<=;i++)
for(int j=i;j<=;j+=i)
fac[j].push_back(i);
//fac[j]里面保存的是长度为j的环的因子
} int main()
{
int T=;
get_fac();
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<m;i++)
scanf("%d",&b[i]);
aa.clear();
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++)
{
if(vis[i])continue;
aa.push_back(dfs(i,a));//aa数组中存下a中每个环的长度
}
memset(vis,false,sizeof(vis));
memset(lenb,,sizeof(lenb));
for(int i=;i<m;i++)
{
if(vis[i])
continue;
lenb[dfs(i,b)]++;
}
long long ans=;
int L=aa.size();
//根据a的每个环去找b约数环
for(int i=;i<L;i++)
{
int lena=aa[i],ll=fac[lena].size();
long long res=;
for(int j=;j<ll;j++)
{
int lb=fac[lena][j];//lb是长度为lena的环的一个因子
res=(res+(long long)lb*lenb[lb])%mod;//乘上长度为这个因子的环的个数
}
ans=(ans*res)%mod;
}
printf("Case #%d: %lld\n",T++,ans);
}
return ;
}

最新文章

  1. Castle Core 4.0.0 alpha001发布
  2. JVM中对象的销毁
  3. 加入一个线程:join
  4. ruby 学习笔记 2 -变量
  5. [转]优秀Python学习资源收集汇总
  6. angular入门系列教程目录
  7. JavaScript 计算两个颜色叠加值
  8. Codeforces Round #258 (Div. 2)[ABCD]
  9. unix c 09
  10. yum安裝的包如何保留到本地
  11. math对象与数组对象
  12. 发运模块中如何创建Debug 文件
  13. Java学习图
  14. tmunx error:invalid option: status-utf8 invalid option: utf8
  15. Linux内核分析 读书笔记 (第三章)
  16. Linux下批量修改文件名(rename)
  17. 设置Nginx以列表方式显示网站内容
  18. POJ 3273 Monthly Expense(二分搜索)
  19. Oracle数据库之PL/SQL程序基础设计
  20. Hadoop HBase概念学习系列之HBase表的一些设置(强烈推荐好好领悟)(十三)

热门文章

  1. element upload 一次性上传多张图片(包含自定义上传不走action)
  2. 安卓——Activity生命周期、
  3. PAT 1013 Battle Over Cities
  4. [codechef July Challenge 2017] Calculator
  5. 一、持久层框架(Hibernate)
  6. 1.Liunx 文件管理
  7. dp 入门
  8. 解决QPainter::drawText修改文字方向
  9. 小程序证书申请FAQ
  10. LY.JAVA面向对象编程.修饰符