Function

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1034    Accepted Submission(s): 464

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
 
Source
题意:
长度为n的数列a[0...n-1]和长度为m的数列b[0...m-1],求有多少个这样的函数f使得 f(i)=bf(ai),i属于[0,n-1];
代码:
//可以看出f在定义域[0,n-1]中是循环的,f的每个循环节中只要有一个f的值确定了那么其他的f的值也就确定了(
//因为每相邻的两个f都相关),所以先找出a数列的所有的循环节然后在b中找f可以对应的值(同样是循环节),只有b
//的某个循环节是a的某个循环节的因子时这两个循环节才能匹配,统计能匹配的个数,结果相乘。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=;
const ll mod=1e9+;
int n,m,a[maxn],b[maxn];
int num1[maxn],num2[maxn];//num1[i]表示a数列第i个循环节的大小,num2[i]表示b数列长度为i的循环节的个数
int main()
{
int cas=;
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]);
memset(num2,,sizeof(num2));
memset(num1,,sizeof(num1)); int tot=;
for(int i=;i<n;i++){
if(a[i]==-) continue;
int ii=i;
tot++;
while(a[ii]!=-){
num1[tot]++;
int t=ii;
ii=a[ii];
a[t]=-;
}
}
for(int i=;i<m;i++){
if(b[i]==-) continue;
int ii=i,len=;
while(b[ii]!=-){
len++;
int t=ii;
ii=b[ii];
b[t]=-;
}
num2[len]++;
}
ll sum=;
for(int i=;i<=tot;i++){
ll cnt=;
for(int j=;j<=num1[i];j++){
if(num1[i]%j==){
cnt+=num2[j]*j;
cnt%=mod;
}
}
sum*=cnt;
sum%=mod;
}
printf("Case #%d: %lld\n",++cas,sum);
}
return ;
}

HDU 6038的更多相关文章

  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. 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 ...

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

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

  4. HDU 6038 17多校1 Function(找循环节/环)

    Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...

  5. 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 ...

  6. hdu 6038 Function

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

  7. HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest - Team 1 1006)

    学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是 ...

  8. 【hdu 6038】Function

    [Link]:http://codeforces.com/contest/834/problem/C [Description] 给你两个排列a和b; a排列的长度为n,b排列的长度为m; a∈[0. ...

  9. HDU 6038 Function —— 2017 Multi-University Training 1

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

随机推荐

  1. 2019CSUST集训队选拔赛题解(三)

    PY学长的放毒题 Description 下面开始PY的香港之行,PY有n个要去的小吃店,这n个小吃店被m条路径联通起来. PY有1个传送石和n−1个传送石碎片. PY可以用传送石标记一个小吃店作为根 ...

  2. Dilworth定理

    来自网络的解释: 定理内容及其证明过程数学不好看不懂. 通俗解释: 把一个数列划分成最少的最长不升子序列的数目就等于这个数列的最长上升子序列的长度(LIS) EXAMPLE 1   HDU 1257 ...

  3. 1035 Password (20 分)(字符串)

    注意下单复数 #include<bits/stdc++.h> using namespace std; pair<string,string>pa; int main() { ...

  4. sql批量更新

    -----------------更新无锡医院名称 update Opt_DKI_Hospital set centerName =tmp.[医院名称] from Opt_DKI_Hospital h ...

  5. ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    在用c#生成应用程序的时候,读写dbf时,open方法出错 ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 以前这个程序是用着好 ...

  6. UML设计(团队作业6)

    决胜 Poker 一.团队成员 学号 姓名 211606392 郑俊瑜 (队长) 211606327 冉繁盛 211606323 刘世华 211606386 姚皓钰 211606358 陈卓楠 211 ...

  7. 02-JAVA 初始化

    构造器 概念:在创建对象时被自动调用的方法,构造器采用和类名一样的名称 创建对象时,会为其分配存储空间,并调用相应的构造器进行初始化.这就确保了在操作对象之前,这个对象已经被恰当的初始化了. 不接受仁 ...

  8. CDOJ ABCDE dp(前缀和优化)

    题目链接: http://acm.uestc.edu.cn/#/problem/show/1307 ABCDE Time Limit: 1000/1000MS (Java/Others)Memory ...

  9. asp.net如何实现负载均衡方案讨论

    请注意,本文内容分多次修改,如需阅读,请阅读完整,因为早期的观点是不太合理的,后面由于水平进步,已经做了修改! 我的目标是我一个人搭建一个负载均衡网站.不接受这是网络部,或者运维,或者系统部的事情,所 ...

  10. 1029 C语言文法定义

    program à external_declaration | program external_declaration <源程序> ->  <外部声明> |  < ...