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. mysql 全文搜索(转载http://blog.csdn.net/manbujingxin/article/details/6656992)

    前提:mysql只支持英文内容的全文索引,所以只考虑英文的全文搜索.假定数据表名为post,有三列:id.title.content.id是自增长序号,title是varchar,content是te ...

  2. 移动端页面利用好viewport,适配各种宽度屏幕

    最近研究微贷网的移动端代码,发现他们网站在适配不同宽度屏幕的显示情况时,发现他们并不是利用rem单位,而是利用js动态设置mete的viewport来达到适配的效果. 感觉挺不错的,也不需要计算什么东 ...

  3. AWR报告学习示例

    1.  参数1  AAS AAS讲解 elapsed 为 该AWR性能报告的时间跨度 DB_TIME =所有前台session花费在database调用上的总和时间. 注意:前台进程 foregrou ...

  4. 函数使用十一:FTP

    网上很多FTP说明的文档,也很详细,写这玩意是备份一下: 注:有的系统需要配置FTP地址和端口,有的好像没有... SM30->SAPFTP_SERVERS_V,默认端口21. 在做之前可以做个 ...

  5. Windows与Linux端口占用查询及处理

    Windows下端口占用查询 输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是49157,首先找到它. 查看被占用端口对应的PID,输入命令:netstat ...

  6. MAC常用软件工具(随某人个人版)

    1.mac命令行工具(自带升级版) https://ohmyz.sh/ 连接远程服务器地址: 直接输入 ssh -A -p 22 root@IP 如:ssh -A -p 22 root@www.bai ...

  7. Windows下定时任务

    windows计划任务 1.写一个PHP程序,命名为test.php,内容如下所示: <? $fp = fopen("test.txt", "a+"); ...

  8. stund客户端使用结果说明

    stun服务器是用于检测网络类型的重要工具. 源码地址:https://svwh.dl.sourceforge.net/project/stun/stun/0.97/stund-0.97.tgz 或者 ...

  9. cmp的值到底是0还是1还是-1的问题

    返回值不局限于这三个数返回负数,表示第一个参数小于第二个参数返回整数,表示第一个参数大于第二个参数返回0,表示他们相等

  10. jsp 内置对象(五)

    1.Request对象 该对象封装了用户提交的信息,通过调用该对象相应的方法可以获取封装的信息,即使用该对象可以 获取用户提交的信息. 当Request对象获取客户提交的汉字字符时,会出现乱码问题,必 ...