Function

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1610    Accepted Submission(s): 755

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 1e9+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≤1e6, ∑m≤1e6.

 
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
 
 
题目大意:给你数列a,b,然后它们之间有函数关系f(i)=bf(ai) ,求满足要求的映射关系的个数。
思路:如果把f(ai)按照上述函数扩展下去,发现它是循环的,并且a的循环一定是b的循环的整倍数, 那么就可以跑dfs求出a和b的循环节(以及相等循环节的个数),最后求和即可。
 
AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int MOD=1e9+;
const int MAXN=;
int a[MAXN],b[MAXN];
int la[MAXN],lb[MAXN];
bool vis[MAXN];
void dfsa(int t, int l, int *num){
if(vis[t]){
la[l]++;
return;
}
vis[t]=;
dfsa(num[t], l+, num);
}
void dfsb(int t, int l, int *num){
if(vis[t]){
lb[l]++;
return;
}
vis[t]=;
dfsb(num[t], l+, num);
}
int main()
{
int n,m,t=;
while(~scanf("%d%d", &n, &m))
{
for(int i=;i<n;i++)
scanf("%d", a+i);
for(int j=;j<m;j++)
scanf("%d", b+j);
memset(la, , sizeof(la));
memset(lb, , sizeof(lb)); memset(vis, , sizeof(vis));
for(int i=;i<n;i++)
if(!vis[i])
dfsa(i, , a); memset(vis, , sizeof(vis));
for(int i=;i<m;i++)
if(!vis[i])
dfsb(i, , b); int res=;
for(int i=;i<=n;i++){
if(la[i]){
int k=(int)(sqrt(i*1.0)+0.5),tmp=;
for(int j=;j<=k;j++){
if(i%j==){
tmp=(tmp+lb[j]*j%MOD)%MOD;
if(j*j!=i)
tmp=(tmp+lb[i/j]*(i/j)%MOD)%MOD;
}
}
for(int j=;j<la[i];j++)
res=res*tmp%MOD;
}
}
printf("Case #%d: %d\n", ++t, res);
}
return ;
}

  嗯,是个图论题。

HDU 6038 Function —— 2017 Multi-University Training 1的更多相关文章

  1. HDU 6038 - Function | 2017 Multi-University Training Contest 1

    /* HDU 6038 - Function [ 置换,构图 ] 题意: 给出两组排列 a[], b[] 问 满足 f(i) = b[f(a[i])] 的 f 的数目 分析: 假设 a[] = {2, ...

  2. HDU 6168 - Numbers | 2017 ZJUT Multi-University Training 9

    /* HDU 6168 - Numbers [ 思维 ] | 2017 ZJUT Multi-University Training 9 题意: .... 分析: 全放入multiset 从小到大,慢 ...

  3. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  4. hdu 6038 Function

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  5. HDU 6038 Function(思维+寻找循环节)

    http://acm.hdu.edu.cn/showproblem.php?pid=6038 题意:给出两个序列,一个是0~n-1的排列a,另一个是0~m-1的排列b,现在求满足的f的个数. 思路: ...

  6. 2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)

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

  7. HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

    /* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...

  8. HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9

    /* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...

  9. 2017 Chinese Multi-University Training, BeihangU Contest

    2017 Chinese Multi-University Training, BeihangU Contest Add More Zero 思路:log10(2^m) = m*log10(2) 代码 ...

随机推荐

  1. Jupyter Notebook 快捷键(基本)

    Jupyter Notebook 快捷键 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文本:这时的单元框线是绿色的.命令模式,键盘输入运行程序命令:这时的单 ...

  2. iframe的基础应用

    点击top.html里面的按钮,刷新left.html右边的内容 <a href="left.html?id=11111&k=5&b=4"  target=& ...

  3. 机器学习实战笔记-11-Apriori与FP-Growth算法

    Apriori算法 优点:易编码实现:缺点:大数据集上较慢:适用于:数值型或标称型数据. 关联分析:寻找频繁项集(经常出现在一起的物品的集合)或关联规则(两种物品之间的关联关系). 概念:支持度:数据 ...

  4. 鸟哥私房菜学习——centos 7_安装

    下面是我安装时遇到问题后搜索找到的可行办法: 准备工具: 8G左右U盘; 最新版UltraISO; CentOS7光盘镜像; CentOS7的镜像文件,可以在网易的开源镜像站或者阿里云的开源镜像站下载 ...

  5. Throwable -抛出异常类与自定义异常类

    /* 自定义异常类 java提供的异常类,不够我们使用,需要自己定义一些异常类 格式: public class XXXException extends Exception/runtimeExcep ...

  6. Node总结

    一. 基本概念 1.1 DOM DOM(Document Object Model), 把网页转换成JS对象,可以用脚本进行各种操作.浏览器将结构化文档(HTML/XML)解析成一系列的节点形成DOM ...

  7. Linux常用命令操作文档

    Ls命令:列出目录内容 选项 长选项 含义 -a --all 列出所有文件,包括隐藏的文件 -d --directory 指定一个目录 -F --classify 在每个列出的名字后面加上类型指示符( ...

  8. java中关于异常的处理

    初学java的时候,当我们碰到异常时,一般会把异常直接throws抛出去,或则在catch的时候,简单的写一句打印异常信息,但是在实际开发中,是不能这么做的.如果我们将底层的某一个异常简单的print ...

  9. 【学习总结】Python-3-字符串函数split()的妙用

    参考: 菜鸟教程-Python3-Python字符串-split() 语法: str.split(str="", num=string.count(str)) 参数 str -- ...

  10. 微软Visual Studio Code基本特征

    Visual Studio Code它的核心功能还是作为一个代码编辑器.和其他的代码编辑器一样,VScode采取通用的UI和布局,浏览器在左边,显示所有的文件和文件夹,右边你打开的文件的编辑页面. 文 ...