2017年7月25日多校一Function
Function这道题我当时一直很迷,到底怎么来的啊,为什么会这样啊??
然后看了题解才知道,原来是找循环啊。
已知f(i)=b[f(a(i)],则
f(0) = b[f(a[0])] = b[f(2)]
f[1] = b[f(a[1])] = b[f(0)]
f[2] = b[f(a[2])] = b[f(1)]
其实这道题可以转化为求环的问题,有多少种方式可以构成题目要求的环,即b的环可以有多少种方式画成a的环。
第一个样例我们可以知道 a0->a1->a0, a2->a2, b0->b0, b1->b1。
则和a同构的环有b0->b0->b0, b1->b1->b1, b0->b0, b1->b1四个
第二个样例我们可以知道a0->a2->a1->a0 b0->b0 b1->b2->b3->b1
则和a同构的环有b1->b2->b3->b1 b2->b3->b1->b2 b3->b1->b2->b3 b0->b0->b0->b0四个
然后找出所有环的长度,是a环的因子的就加起来,最后将所有的乘起来(为什么是乘起来??因为组合数学啊(●'◡'●))
ll ans=;
for (int i=; i<va.size(); i++) {
ll num=;
for (int j=; j<vb.size(); j++) {
if (va[i]%vb[j]==) num+=vb[j];
}
ans = ans*num%mod;
}
代码:
/* gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e5+;
const ll maxm = 1e7;
const ll mod = 1e9+;
const int INF = <<;
const db eps = 1e-;
int a[maxn], b[maxn];
bool visa[maxn], visb[maxn];
vector<ll>va, vb;
int ca=;
int flag;
void init() {
memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(visa, , sizeof(visa));
memset(visb, , sizeof(visb));
va.clear();
vb.clear();
}
void solve() {
int n, m;
while(scanf("%d%d", &n, &m)!=EOF) {
init();
for (int i=; i<n; i++) {
scanf("%d", &a[i]);
}
for (int i=; i<m; i++) {
scanf("%d", &b[i]);
}
for (int i=; i<n; i++) {
if (visa[i]) continue;
int x=i; int sum=;
while(x!=a[x]) {
if (visa[x]) break;
visa[x]=;
sum++;
// cout<<x<<" "<<a[x]<<endl;
x=a[x];
}
//cout<<x<<endl;
if (x==i) {
// cout<<"sum:"<<sum<<endl;
if (sum==) va.push_back();
else va.push_back(sum);
} else {
x=i; int num=;
while(x!=a[x]) {
if (num>=sum) break;
visa[x]=;
num++;
x=a[x];
}
visa[i]=; va.push_back();
}
}
for (int i=; i<m; i++) {
if (visb[i]) continue;
int x=i; int sum=;
while(x!=b[x]) {
if (visb[x]) break;
visb[x]=;
sum++;
x=b[x];
}
if (x==i) {
if (sum==) vb.push_back();
else vb.push_back(sum);
} else {
x=i; int num=;
while(x!=a[x]) {
if (num>=sum) break;
visb[x]=;
num++;
x=b[x];
}
visb[i]=; vb.push_back();
}
}
ll ans=;
for (int i=; i<va.size(); i++) {
ll num=;
for (int j=; j<vb.size(); j++) {
if (va[i]%vb[j]==) num+=vb[j];
}
ans = ans*num%mod;
}
printf("Case #%d: %lld\n", ca++, ans);
}
}
int main() {
//freopen("in.txt","r",stdin);
// freopen("kingdom.in","r",stdin);
//freopen("kingdom.out","w",stdout);
int t=;
// scanf("%d", &t);
while(t--) solve();
}
2017年7月25日多校一Function的更多相关文章
- 2017年3月25日工作日志:Jquery使用小结[绑定事件判断、select标签、军官证正则]
jQuery获取DOM绑定事件 在1.8.0版本之前,我们要想获取某个DOM绑定的事件处理程序可以这样: $.data(domObj,'events');//或者$('selector').data( ...
- 猖獗的假新闻:2017年1月1日起iOS的APP必须使用HTTPS
一.假新闻如此猖獗 刚才一位老同事 打电话问:我们公司还是用的HTTP,马上就到2017年了,提交AppStore会被拒绝,怎么办? 公司里已经有很多人问过这个问题,回答一下: HTTP还是可以正常提 ...
- [转载]Ubuntu17.04(Zesty Zapus)路线图发布:2017年4月13日发布
Canonical今天公布了Ubuntu 17.04(Zesty Zapus)操作系统的发布路线图,该版本于今年10月24日上线启动,toolchain已经上传且首个daily ISO镜像已经生成.面 ...
- 2017年1月5日 星期四 --出埃及记 Exodus 21:31
2017年1月5日 星期四 --出埃及记 Exodus 21:31 This law also applies if the bull gores a son or daughter.牛无论触了人的儿 ...
- 2017年1月4日 星期三 --出埃及记 Exodus 21:30
2017年1月4日 星期三 --出埃及记 Exodus 21:30 However, if payment is demanded of him, he may redeem his life by ...
- 2017年1月3日 星期二 --出埃及记 Exodus 21:29
2017年1月3日 星期二 --出埃及记 Exodus 21:29 If, however, the bull has had the habit of goring and the owner ha ...
- 2017年1月2日 星期一 --出埃及记 Exodus 21:28
2017年1月2日 星期一 --出埃及记 Exodus 21:28 "If a bull gores a man or a woman to death, the bull must be ...
- 2017年1月1日 星期日 --出埃及记 Exodus 21:27
2017年1月1日 星期日 --出埃及记 Exodus 21:27 And if he knocks out the tooth of a manservant or maidservant, he ...
- 2016年12月25日 星期日 --出埃及记 Exodus 21:20
2016年12月25日 星期日 --出埃及记 Exodus 21:20 "If a man beats his male or female slave with a rod and the ...
随机推荐
- mysql分组GROUP BY常用sql
数据分组 GROUP BY GROUP BY可以根据一个或多个字段进行分组. 比如,根据prod_id分组: SELECT prod_id ,user_id FROM products GROUP B ...
- with as 如何工作
with as 如何工作 with如何工作? Python对with的处理还是很机智滴.基本思想就是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法 紧跟wi ...
- Gym - 101911C Bacteria (规律题)
传送门:点我 Time limit2000 ms Memory limit262144 kB Recently Monocarp has created his own mini-laboratory ...
- cross-env:跨平台设置和使用环境变量
一 项目结构 二 安装依赖 npm install --save-dev cross-env 三 npm脚本 { "name": "demo", "v ...
- TZOJ 3295 括号序列(区间DP)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
- [剑指Offer]24-反转链表
题目链接 https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&t ...
- 《centos系列》配置vim编辑器
直接wget到~/目录下: wget http://files.cnblogs.com/ma6174/vimrc.zip 然后进行解压: unzip -f vimrc.zip -d ~/ 参 ...
- Android 中 IntentService 的优点
简而言之:可以处理异步请求,任务完成会自动停止自己. IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要 ...
- 线特征---LineMatching原理(四)
参考文章:An efficient and robust line segment matching approach based on LBD descriptor and pairwise geo ...
- 整站变灰CSS代码
* { filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); -webkit-filter: grayscale(100% ...