HDU 6038 Function (多校1)
来源:互联网 发布:mac重装没有磁盘 编辑:程序博客网 时间:2024/06/10 18:25
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 21 0 20 13 42 0 10 2 3 1
Sample Output
Case #1: 4Case #2: 4
题目大意
有两个数列 a(0~n-1)和b(0~m-1),求满足 f(i)=bf(ai)函数f 的个数。
解题思路
以样例1 为例,对于序列a: a0=1,a1=0,a2=2,根据函数 f(i) 可得:
f(0)=bf(a0)=bf(1)
f(1)=bf(a1)=bf(0)
由f(1)可得:f(0)=bbf(0)
由上式可以说明 f(0) 在序列 b 中运算(循环)两次又得到 f(0) 的数值,且其他函数值(如f(1))便可由f(0)的取值唯一确定。
f(2)=bf(a2)=bf(2)
该式说明在数列 b 中 b[f(2)]=f(2),即运算(循环)一次便是本身。
这个循环的次数即为循环节,即序列 a 存在 1 个循环节长度为 1 的序列和 1 个循环节长度为 2 的序列。
对于任意 i ,f(i)=bf(ai)=bbf(aai)=……=bbb…bf(i)(共有 l 个b), 即对于此序列的循环节的长度为 l 。
在 b 序列中,只有其循环节长度为 l 的因子时才能满足上式。如:
对于序列 b : a[0]=0,a[1]=1,即存在两个循环节长度为 1 的序列,则对于 a 中循环节长度为 2 的序列,f(0)可取值为 0 或 1,f(1)则相应确定,如当取 0 所在的序列时,对于<1>式则有f(0)=0,bf(0)=0(f(1)=0),bf(0)=0,即满足等式;对于 a 中循环节长度为 1 的序列,f(2) 同样可以取值为 0 或 1;最后对于 a 中不同循环节对应的取值情况相乘即可(即2*2)。
由此可以推出计算的公式 ans=
代码实现
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define ll long long#define maxn 1000007const int mod=1e9+7;int a[maxn],b[maxn],visit[maxn];ll len1[maxn],len2[maxn];void cal(int *num,int l,ll *res){ int i,j,k; ll length; memset(visit,0,sizeof(visit)); for(i=0; i<l; i++) { if(!visit[i]) { length=1,j=i; visit[i]=1; if(num[i]!=i) { while(1) { k=num[j]; visit[k]=1; j=k; length++; if(num[k]==i) break; } } res[length]++; //res[length]标记length长度出现的次数 res[0]=max(res[0],length); //标记循环节长度的上限 } }}int main(){ int m,n,cas=0; ll ans; while(~scanf("%d %d",&m,&n)) { ans=1; memset(len1,0,sizeof(len1)); memset(len2,0,sizeof(len2)); for(int i=0; i<m; i++) scanf("%d",&a[i]); for(int i=0; i<n; i++) scanf("%d",&b[i]); cal(a,m,len1); cal(b,n,len2); for(int i=1;i<=len1[0];i++) { if(len1[i]==0) continue; ll re=0; for(int j=1;j<=len2[0];j++) { if(len2[j]==0) continue; if(i%j==0) { re=(re+((ll)j*len2[j]%mod))%mod; } } for(int k=1;k<=len1[i];k++) ans=(ans*re)%mod; } printf("Case #%d: %lld\n",++cas,ans); } return 0;}
PS:
写代码思路还是不是很清晰,不能灵活的运用STL。改进改进
- HDU 6038 Function (多校1)
- HDU 6038 Function
- HDU-6038 Function
- hdu 6038 Function(思维)
- HDU 6038 Function
- hdu 6038 Function
- HDU 6038 Function
- hdu 6038 Function
- HDU 6038 Function
- Function (HDU 6038)
- HDU 6038 Function【思维】
- hdu--6038--Function
- HDU 6038-Function
- hdu 6038 Function
- hdu 6038 Function
- Function HDU-6038
- HDU-6038 Function(思维)
- HDU 6038 Function (数学)
- SQL经典题目
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA
- HDU
- 2017IMO第五题
- 循环
- HDU 6038 Function (多校1)
- Hdu图论最短路基础题
- VS工程文件常见后缀名文件含义
- 双系统下Ubuntu16.04与Windows10时间不同步解决办法
- nginx反向代理和负载均衡
- Vijos[1983]NOIP2015Day2T3 运输计划 transport LCA
- Tensorflow中TFRecord格式介绍
- robot framework 如何灵活地执行测试用例和测试套件啊
- C++map