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 ;
}

HDU 6038 17多校1 Function(找循环节/环)的更多相关文章

  1. 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

    Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...

  2. Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

    题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...

  3. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  4. 2016"百度之星" - 初赛(Astar Round2A)1001 All X(HDU5690)——找循环节|快速幂

    一个由m个数字x组成的新数字,问其能否mod k等于c. 先提供第一种思路,找循环节.因为每次多一位数都是进行(t*10+x)mod k(这里是同余模的体现),因为x,k都确定,只要t再一样得到的答案 ...

  5. HDU 6050 17多校2 Funny Function(数学+乘法逆元)

    Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7.   ...

  6. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  7. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  8. HDU 6103 17多校6 Kirinriki(双指针维护)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...

  9. HDU 6060 17多校3 RXD and dividing(树+dfs)

    Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...

随机推荐

  1. Object.keys的使用

    链接:https://www.nowcoder.com/questionTerminal/52c41b84e32a4158883cb112a1d1f850来源:牛客网 输出对象中值大于2的key的数组 ...

  2. node模块之path——path.join和path.resolve的区别

    1.path.join([...paths]) path.join() 方法使用平台特定的分隔符把全部给定的 path 片段连接到一起,并规范化生成的路径. 长度为零的 path 片段会被忽略. 如果 ...

  3. MySQL 分页查询和存储过程

    一.分页查询 使用limit函数,limit关键字的用法: LIMIT [offset,] rows: offset指定要返回的第一行的偏移量,rows第二个指定返回行的最大数目.初始行的偏移量是0( ...

  4. HTML标签(二)

    一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...

  5. [洛谷 P2709] 小B的询问

    P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...

  6. SQL SERVER版本补丁体系及升级

    首先了解几个定义: RTM : 表示 Release to Manufacturing ,这是产品的原始发布版本,当从光盘或 MSDN 下载的默认版本.不过现在下载 SQL Server 版本时,也有 ...

  7. 从线程模型的角度看Netty的高性能

    转载:Netty(二) 从线程模型的角度看 Netty 为什么是高性能的? 传统 IO 在 Netty 以及 NIO 出现之前,我们写 IO 应用其实用的都是用 java.io.* 下所提供的包. 比 ...

  8. Win10系列:JavaScript综合实例4

    实现主页面和分类页面的之后,最后来看一下菜肴页面的实现,这个页面用于详细介绍某项菜肴或主食,如名称.图片和具体做法等.在pages文件夹里面添加一个名为foodDetail的文件夹,并在foodDet ...

  9. Linux系统从零到高手的进阶心得

    初次了解到Linux系统还是在我初中的时候,那时候正是在一个中二年龄,喜欢看小说,对于小说中出现的明显的非现实场景感到十分钦佩.羡慕,并常常幻想自己也有小说主人公那样的本领.那正是在这样一个充满幻想的 ...

  10. Resharper插件如何启用原VS的智能提示

    第一步:vs2015选择工具—>选项—>文本编辑器—>C#—>常规—>语句结束,勾选自动列出成员,如下图: 第二步: 关闭Resharper智能提示,并设置为Visual ...