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实现基于两张图片生成圆角图标效果的方法

    python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...

  2. 解读vue filter

    1.全局filter, 全局的过滤一般在main.js里面使用 <div id="app"> <div> {{testVal | filVal(10,30) ...

  3. 设置div标签可以输入文字

    1.contenteditable 属性可以设置div标签为克输入标签,   2.input和textarea虽然是常用的输入标签,但是这两个标签不能设置最大高度和最小高度, 随意如果想随着输入的内容 ...

  4. Python入门习题6.猜数游戏和其异常处理

    例6:在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了”:小于预设的数,显示“遗憾,太小了”,如此循环,直至猜中数,显示”预测N次,猜中了”,其中N是 ...

  5. CodeChef Sereja and GCD

    Sereja and GCD   Problem code: SEAGCD   Submit All Submissions   All submissions for this problem ar ...

  6. 项目使用Kafka镜像报错处理记录:this server does not host this topic-partition

    背景 项目使用docker swarm部署 服务之间使用消息中间件 kafka 通信 Kafka 使用 star 3.7k 的 wurstmeister/kafka:2.12-2.2.1 镜像 Zoo ...

  7. C# form 传参数的几个方法

    方法一:传值最先想到的,Form2构造函数中接收一个string类型参数,即Form1中选中行的文本,将Form2的TextBox控件的Text设置为该string,即完成了Form1向Form2的传 ...

  8. 让 Git Bisect 帮助你

    让 Git Bisect 帮助你 英文原文:Letting Git Bisect Help You   Git 提供来很多的工具来帮助我们改进工作流程. bisect 命令就是其中之一, 虽然由于使用 ...

  9. 攻防世界--dmd-50

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/7ef7678559ea46cbb535c0b6835f2f4d 1.准备 获取信息 6 ...

  10. 本机ip地址怎么查

     转自:https://www.192ly.com/basic/local-ip-address-lookup-method.html 百度搜索一下[IP],你就可以轻松看到你的IP地址了,百度出来的 ...