HDU 6038 Function (数学)

网友投稿 540 2022-11-09

HDU 6038 Function (数学)

HDU 6038 Function (数学)

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 10^9+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 21 0 20 13 42 0 10 2 3 1

Sample Output

Case #1: 4Case #2: 4

题意

给出两个序列 a 和 b ,求满足 f(i)=bf(ai)

思路

根据样例 1 我们可以得到:

f(0)=bf(1)

f(1)=bf(0)

f(2)=bf(2)

代换以后有 f(0)=bf(1)=bbf(0) , f(1)=bf(0)=bbf(1)

考虑置换 a 的一个循环节,长度为 l ,那么有 f(i)=bf(ai)=bbf(aai)=b⋯bf(i)l times b

于是 f(i) 的值在置换 b 中所在的循环节的长度必须为 l

而如果 f(i) 的值确定下来了,这个循环节的另外 l−1

因此满足条件的函数个数就是 ∏ki=1∑j|lij⋅calj ,其中 k 是置换 a 中循环节的个数, li 表示置换 a 中第 i 个循环节的长度, calj 表示置换 b 中长度为 j

AC 代码

#include#include#include#include#include#includeusing namespace std;typedef __int64 LL;const int maxn=110000;const int mod = 1e9+7;int a[maxn],b[maxn];vector na,nb;bool vis[maxn];void findnum(int *a,int n,vector &res) //寻找 res 中的循环节个数以及长度{ memset(vis,false,sizeof(vis)); for(int i=0; i>n>>m) { na.clear(); nb.clear(); for(int i=0; i>a[i]; for(int i=0; i>b[i]; findnum(a,n,na); findnum(b,m,nb); LL ans=1; int lena=na.size(); int lenb=nb.size(); for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决Mybatis中result标签识别不了的情况
下一篇:HDU 5907:Find
相关文章

 发表评论

暂时没有评论,来抢沙发吧~