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. python numpy求四分位距

    import numpy as np ages=[3,3,6,7,7,10,10,10,11,13,30] lower_q=np.quantile(ages,0.25,interpolation='l ...

  2. Mybatis入门之MyBatis基础

    一.MyBatis概述 1.ORM模型简介 ORM:对象关系映射(Object Relation Mapping) 1)传统JDBC程序的设计缺陷(实际项目不使用) a.大量配置信息硬编码 b.大量的 ...

  3. TCP概述

    1. TCP提供的服务 我们知道TCP是一个面向连接.提供可靠数据数据传输服务的传输层协议.面向连接意味着发送端和接收端在交换数据前需要建立一个连接,和我们平常打电话一样,在通话前,需要拨号建立连接. ...

  4. 【转】时间序列分析——基于R,王燕

    <时间序列分析——基于R>王燕,读书笔记 笔记: 一.检验: 1.平稳性检验: 图检验方法:     时序图检验:该序列有明显的趋势性或周期性,则不是平稳序列     自相关图检验:(ac ...

  5. Git011--分支管理策略

    Git--分支管理策略 一.分支管理策略 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,G ...

  6. C# 中定义斜杠 \

    \ 是转义符 如     \’ 单引号    \” 双引号    \\ 反斜杠    \0 空    \a 警告(产生峰鸣)    \b 退格    \f 换页    \n 换行    \r 回车   ...

  7. EasyUI在子tab基础上再打开新的tab标签页

    var title = "xxxx"; var content = '<iframe scrolling="auto" frameborder=" ...

  8. 解决Maven项目BindingException异常

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mybatis.m ...

  9. php提交表单时如何保留多个空格及换行的文本样式

    需求是:用户提交表单时屏蔽敏感词的功能.其中敏感词来自服务器端同一路径下的ciku.txt,敏感词通过"|"连接,例如"g|c|a",提交表单时替换敏感词,更重 ...

  10. JavaScript —— 正则表达式元字符

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...