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−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
题意:
有两个,数组a是[0n-1]的排列,数组b是[0m-1]的排列。现在定义f(i)=b[f(a[i])];
- 问f(i)有多少种取值,使得表达式f(i)=b[f(a[i])]全部合法。
分析:
以第一个样例 a={1,0,2} b={0,1}为例:
那么f(0)=b[f(1)] f(1)=b[f(0)] f(2)=b[f(2)]
这里有两个环分别为 f(0)->f(1) 和f(2)
所以我们的任务就是在b中找环,该环的长度必须为a中环的长度的约数。
为什么必须的是约数呢?
因为如果b的环的长度是a的环的长度的约数的话,那也就意味着用b这个环也能构成a这个环,只不过是多循环了几次而已。
然后找到a中所有环的方案数,累乘便是答案。
为什么要累乘呢?我最开始一直以为要累加。
这个就用到了排列组合的思想,因为肯定要f(i)肯定要满足所有的数,而a中的每个环都相当于从a中取出几个数的方案数,所以总共的方案数应该累乘。
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
const int max_n=100010;
const int mod=1e9+7;
int n,m;
int a[max_n],b[max_n],len_b[max_n];
bool vis[max_n];
vector <int>A,fac[max_n];
///构建环,并返回环的大小
int dfs(int N,int *c)
{
if(vis[N])
return 0;
vis[N]=1;
return dfs(c[N],c)+1;
}
void get_fac()
{
for(int i=1; i<=100000; i++)///fac[j]里面保存的是长度为j的环的因子
{
for(int j=i; j<=100000; j+=i)
fac[j].push_back(i);
}
}
int main()
{
int Case=0;
get_fac();
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
for(int i=0; i<m; i++)
scanf("%d",&b[i]);
A.clear();
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
{
if(vis[i]) continue;
A.push_back(dfs(i,a));///a数组中环的长度,重复的长度也是保存的
}
memset(vis,0,sizeof(vis));
memset(len_b,0,sizeof(len_b));
for(int i=0; i<m; i++)
{
if(vis[i]) continue;
len_b[dfs(i,b)]++;///b数组中长度为dfs(i,b)的环的个数
}
long long int ans=1;
for(int i=0,L=A.size(); i<L; i++)
{
int la=A[i];///取出a中的一个长度为la的环
long long res=0;
for(int j=0,ll=fac[la].size(); j<ll; j++)
{
int lb=fac[la][j];///lb是长度为la的环的一个因子
res=(res+(long long )lb*len_b[lb])%mod;///因子个数乘以环的个数就是一功德方案数
}
ans=ans*res%mod;
}
printf("Case #%d: %lld\n",++Case,ans);
}
}
2017ACM暑期多校联合训练 - Team 1 1006 HDU 6038 Function (排列组合)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
随机推荐
- Scrum Meeting Beta - 2
Scrum Meeting Beta - 2 NewTeam // 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 了解缓存的相关内容Issue #109 设计本地存储的方案Is ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-6
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 完成对账单的编辑,删除等操作,以及开始服务器的编写工作 明天计划完成的工作: 记账功能基本完成,进一步优化功能与完善服务器 工作中遇到的困难 ...
- 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)
有一些网站只能在IE浏览器里打开,不像firefox那样有好多好用的插件来找元素的xpath,css path等. 当然现在IE也可以,F12出现像firebug那样的窗口,来查看元素. 这里呢在介绍 ...
- windows下面安装python3遇到的没有添加到环境变量的问题
windows下面安装python3出现的问题 在官网上面下载最新版的安装包进行安装,并勾选Add Python 3.5 to PATH 安装的过程中可能会出现没有添加到PATH路径的情况 默认的安装 ...
- php 多维数组排序
PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...
- C++变量内存分配及类型修饰符
前言 了解C++程序内存分配,有助于深刻理解变量的初始化值以及其生存周期.另外,变量类型修饰符也会影响到变量的初始化值及其生存周期.掌握了不同类型变量的初始化值及其生存周期,能够让我们设计程序时定义变 ...
- Halcon 笔记2 Blob分析
1. 数组操作 2. 可视化-更新窗口 (1)单步模式-总是:则可以自动显示图像: (2)单步模式-从不:需要调用显示函数才能显示图像. (3)单步模式-清空显示:将原图清除,再显示新图 3. 图像处 ...
- Java Servlet异步处理、非阻塞I/O和文件上传
异步处理 应用服务器中的 web容器通常对各个客户端情求分别使用一个服务器线程.在工作负载很繁重的情况下,容器常要大量线程来为所有客户端请求服务.可扩展性限制包括内存用尽,或容器线程池耗尽.为了创建可 ...
- OSPF协议介绍及配置 (上)
OSPF协议介绍及配置 (上) 一.OSPF概述 回顾一下距离矢量路由协议的工作原理:运行距离矢量路由协议的路由器周期性的泛洪自己的路由表,通过路由的交互,每台路由器都从相邻的路由器学习到路由,并且加 ...
- java多线程 - 学习笔记
------------------------------------------------------------- sleep()与wait() sleep是线程类(Thread)的方法,wa ...