hdu 6038 Function(思维)
来源:互联网 发布:python telnetlib详解 编辑:程序博客网 时间:2024/06/09 17:52
Function
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 501 Accepted Submission(s): 195
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 functionf 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 functionsf 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 from0 to n−1 mapped into different integers in these two functions.
The answer may be too large, so please output it in modulo109+7 .
Define that the domain of function
Please calculate the quantity of different functions
Two functions are different if and only if there exists at least one integer from
The answer may be too large, so please output it in modulo
Input
The input contains multiple test cases.
For each case:
The first line contains two numbersn, m . (1≤n≤100000,1≤m≤100000)
The second line containsn numbers, ranged from 0 to n−1 , the i -th number of which represents ai−1 .
The third line containsm numbers, ranged from 0 to m−1 , the i -th number of which represents bi−1 .
It is guaranteed that∑n≤106, ∑m≤106 .
For each case:
The first line contains two numbers
The second line contains
The third line contains
It is guaranteed that
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
通过观察f[i]=bf(a[i]),如果知道了f[i]的值就知道了a[i[=i的值,就知道了bf(i)的值,如果形成环,就在b数组里用同样的方法找环并且环的大小是a的环的约数
即可以使a中的环成立即为一种方案,由于是定义域到值域的映射,每一个x->y是不同的,所以一定会有环
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <bits/stdc++.h>using namespace std;const int N = 1e6+7;typedef long long LL;const LL mod = 1e9+7;vector<int>p1[N],p2[N];LL a[N], b[N];int d1[N], d2[N];int vis1[N], vis2[N], cnt1, cnt2;void dfs1(int u,int step,int fa){ vis1[u]=1, d1[u]=step; for(int i=0;i<p1[u].size();i++) { int v=p1[u][i]; //if(v==fa) continue; if(vis1[v]) { a[cnt1++]=d1[u]-d1[v]+1; } else dfs1(v,step+1,u); } return ;}void dfs2(int u,int step,int fa){ vis2[u]=1, d2[u]=step; for(int i=0;i<p2[u].size();i++) { int v=p2[u][i]; // if(v==fa) continue; if(vis2[v]) { b[cnt2++]=d2[u]-d2[v]+1; } else dfs2(v,step+1,u); } return ;}int main(){ int ncase=1; int n, m; while(scanf("%d %d", &n, &m)!=EOF) { for(int i=0;i<=max(n,m);i++) p1[i].clear(),p2[i].clear(); for(int i=0;i<n;i++) { int x; scanf("%d", &x); //p1[x].push_back(i); p1[i].push_back(x); } for(int i=0;i<m;i++) { int x; scanf("%d", &x); //p2[x].push_back(i); p2[i].push_back(x); } memset(vis1,0,sizeof(vis1)); memset(vis2,0,sizeof(vis2)); cnt1=0, cnt2=0; for(int i=0;i<n;i++) if(!vis1[i]) dfs1(i,0,-1); for(int i=0;i<m;i++) if(!vis2[i]) dfs2(i,0,-1); LL sum=1; for(int i=0;i<cnt1;i++) { LL cnt=0; for(int j=0;j<cnt2;j++) { if(a[i]%b[j]==0) cnt=(cnt+b[j])%mod; } sum=(sum*cnt)%mod; } printf("Case #%d: %lld\n",ncase++,sum); } return 0;}
阅读全文
0 0
- hdu 6038 Function(思维)
- HDU 6038 Function【思维】
- HDU-6038 Function(思维)
- hdu 6038 Function (思维题搞一搞)
- HDU 6038(Function) 思维 Java
- Hdu 6038 Function【思维+强连通找环】
- 2017多校第一场 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
- 【TFS定制】TFS模板编辑:简单操作介绍
- LinkedHashMap实现缓存
- 网络流算法--FORD-FULKERSON方法及其多种实现
- u3d知识点
- Mac OS搭建python环境下的Selenium web测试环境
- hdu 6038 Function(思维)
- CSS基础汇总
- h5学习笔记:vuethink 配置
- Minigame Learning for Day 5
- POJ 3096 : Surprising Strings
- Can you find it? --CSU-ACM2017暑假集训2-二分搜索
- Head First Servlet & JSP 学习笔记(1)
- 次方求模 NYOJ
- Git-创建与合并分支