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 ...
随机推荐
- 全国高校绿色计算大赛 预赛第一阶段(C++)第1关:将字符串反转
挑战任务 将输入的字符串str进行反转. 编程要求 补全右侧vector<char> inversion(string str)函数实现字符串的反转并返回反转之后的字符串,其中函数参数st ...
- 第九章 词典 (d2)散列:排解冲突(2)
- C/C++中#pragma once的使用
在C/C++中,为了避免同一个文件被include多次,有两种方式:一种是#ifndef方式,一种是#pragma once方式(在头文件的最开始加入). #ifndef SOME_UNIQUE_NA ...
- JavaScript各种继承方式(二):借用构造函数继承(constructor stealing)
一 原理 在子类的构造函数中,通过call ( ) 或 apply ( ) 的形式,调用父类的构造函数来实现继承. function Fruit(name){ this.name = name; th ...
- linux同步机制
很早之前就接触过同步这个概念了,但是一直都很模糊,没有深入地学习了解过,近期有时间了,就花时间研习了一下<linux内核标准教程>和<深入linux设备驱动程序内核机制>这两本 ...
- TableView中Label自适应高度
//Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableVi ...
- linux-ubuntu 安装配置Redis
>wget http://download.redis.io/releases/redis-3.2.6.tar.gz #下载redis源码包 >tar -zxvf redis-3.2.6. ...
- DialogActivity
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=&q ...
- sqljdbc4.jar的安装
自己项目环境(idea+jdk1.8+tomcat8),在搭建maven项目时,由于本地数据库是使用了sqlserver,所以需要项目与sqlserver之间建立连接,但是网上查的资料都说微软不允许以 ...
- An enumerable sequence of parameters (arrays, lists, etc) is not allo
环境:dapper asp.net core 出错代码如下: public Task<IEnumerable<dynamic>> GetList(string query, p ...