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. sparklyr-R语言访问Spark的另外一种方法

    Connect to Spark from R. The sparklyr package provides a complete dplyr backend. Filter and aggregat ...

  2. Mac 安装md5sum等

    一.安装md5sum 方案1.使用brew 安装 方案2.使用源码编译安装 源码下载地址:http://www.microbrew.org/tools/md5sha1sum/md5sha1sum-0. ...

  3. oracle 自动备份

    此次操作是每分钟备份一张表到新表(测试) 准备: 有一张表name是test 注意事项: 1.任务中调用需要显示声明权限 AUTHID CURRENT_USER 或赋予相应权限 2.单独调用过程成功, ...

  4. InnoDB存储引擎介绍-(4)Checkpoint机制二

    原文链接 http://www.cnblogs.com/chenpingzhao/p/5107480.html 一.简介 思考一下这个场景:如果重做日志可以无限地增大,同时缓冲池也足够大,那么是不需要 ...

  5. 精通Oracle的关键是……(Ask Tom上最经常被问到的问题)(转)

    原文地址:http://www.ituring.com.cn/article/37548 这是我在asktom上最经常收到的问题:我需要怎么做才能变成一个专家呢?关于Oracle,有这样的一个关键事物 ...

  6. 【LeetCode】跳跃游戏

    给定一组非负整数,初始时处于数组的第一位下标 0 的位置,数组的每个元素代表那个位置可以跳跃的最大长度.判断你是否能够到达数组的最后一位下标. e.g. A = [2, 3, 1, 1, 4],返回 ...

  7. [luogu P3195] [HNOI2008]玩具装箱TOY

    [luogu P3195] [HNOI2008]玩具装箱TOY 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆, ...

  8. HashSet和ArrayList有什么区别

    hashSet存储的是无序,不可重复,无索引 ArrayList存储的是有序,可重复,有索引

  9. Xcode下的中文乱码问题

    Xcode下的中文乱码问题 转载自:http://linyehui.me/2014/07/09/convert-gbk-to-utf8-on-mac.html =========== 问题原因 绝大部 ...

  10. JBOSS禁用delete和put方法教程

    一.背景说明(与此节修复没多大关系可跳过) 今天应用报扫描出“启用不安全的HTTP方法”漏洞需要进行修复,看后边还有IIS的修复建议:一边不满怎么用IIS一边研究了具体操作半天,由于IIS不同版本操作 ...