topcoder srm 625 div1

problem1 link

假设第$i$种出现的次数为$n_{i}$,总个数为$m$,那么排列数为$T=\frac{m!}{\prod_{i=1}^{26}(n_{i}!)}$

然后计算回文的个数,只需要考虑前一半,得到个数为$R$,那么答案为$\frac{R}{T}$.

为了防止数字太大导致越界,可以分解为质因子的表示方法。

problem2 link

假设终点所在的位置为$(tx,ty)$,那么所有底面是$1x1$的格子$(x,y)$一定满足$(x-tx)mod(3)=0,(y-ty)mod(3)=0$

把每个这样的点拆成两个点然后建立最小割的图。

源点与所有的$b$相连,终点与汇点相连,流量为无穷,割边不会在这里产生。

如果不是洞,那么这个格子拆成的两个点流量为1,表示将这个格子设为洞。

每个格子向周围连边,流量为将中间的两个格子设为洞的代价。

最后最小割就是答案。

problem3 link

首先考虑集合之间的关系。设$f[i][j]$表示前$i$个人分成$j$个集合的方案数。初始化$f[1][1]=n$。那么有:

(1)$f[i+1][j+1]=f[i][j]*j$表示新加一个集合,可以在任意两个集合之间

(2)$f[i+1][j]=f[i][j]*j*2$表示新加的元素与之前的某一个集合在一起,可以放在那个集合的前后,所以有$j*2$种方法

(3)$f[i+1][j-1]=f[i][j]*j$表示合并两个集合,可以在任意两个集合之间插入从而进行合并

最后就是对于$f[x][y]$来说,有多少种方式可以在$n$个位置上放置$x$个使得有$y$个集合并且任意两个集合不相邻。令$m=n-(x-y)$,那么相当于在$m$个位置中放置$y$个,使得任意两个不相邻。由于$f[1][1]=n$那么这$y$个集合的排列已经计算了,所以现在可以假设这$y$个元素的第一个放在$m$个位置的第一个位置,那么第二个位置也不能放置了。所以还剩$m-2$个位置,$y-1$个元素。由于每放置一个元素其后面的位置就不能放置了,所以可以把剩下$y-1$个元素的位置与其后面相邻的位置绑定成一个位置,这样的话,就是$m-2-(y-1)$个位置,$y-1$个元素,即$C_{m-2-(y-1)}^{y-1}=C_{n-(x-y)-2-(y-1)}^{y-1}=C_{n-x-1}^{y-1}$

code for problem1

#include <cmath>
#include <string>
#include <vector>class PalindromePermutations {public:double palindromeProbability(const std::string &word) {std::vector<int> h(26, 0);for (auto e : word) {++h[e - 'a'];}int old_idx = -1;for (int i = 0; i < 26; ++i) {if (h[i] % 2 == 1) {if (old_idx != -1) {return 0.0;}old_idx = i;}}auto total = Compute(h);if (old_idx != -1) {--h[old_idx];}for (auto &e : h) {e /= 2;}auto target = Compute(h);double result = 1.0;for (int i = 2; i < 50; ++i) {result *= std::pow(i, target[i] - total[i]);}return result;}private:std::vector<int> Compute(const std::vector<int> &h) {std::vector<int> result(50, 0);auto Add = [&](int x, int sgn) {for (int i = 2; i <= x; ++i) {int k = i;for (int j = 2; j * j <= k; ++j) {while (k % j == 0) {result[j] += sgn;k /= j;}}if (k != 1) {result[k] += sgn;}}};int n = 0;for (auto e : h) {Add(e, -1);n += e;}Add(n, 1);return result;}
};

code for problem2

#include <limits>
#include <unordered_map>
#include <vector>template <typename FlowType>
class MaxFlowSolver {static constexpr FlowType kMaxFlow = std::numeric_limits<FlowType>::max();static constexpr FlowType kZeroFlow = static_cast<FlowType>(0);struct node {int v;int next;FlowType cap;};public:int VertexNumber() const { return used_index_; }FlowType MaxFlow(int source, int sink) {source = GetIndex(source);sink = GetIndex(sink);int n = VertexNumber();std::vector<int> pre(n);std::vector<int> cur(n);std::vector<int> num(n);std::vector<int> h(n);for (int i = 0; i < n; ++i) {cur[i] = head_[i];num[i] = 0;h[i] = 0;}int u = source;FlowType result = 0;while (h[u] < n) {if (u == sink) {FlowType min_cap = kMaxFlow;int v = -1;for (int i = source; i != sink; i = edges_[cur[i]].v) {int k = cur[i];if (edges_[k].cap < min_cap) {min_cap = edges_[k].cap;v = i;}}result += min_cap;u = v;for (int i = source; i != sink; i = edges_[cur[i]].v) {int k = cur[i];edges_[k].cap -= min_cap;edges_[k ^ 1].cap += min_cap;}}int index = -1;for (int i = cur[u]; i != -1; i = edges_[i].next) {if (edges_[i].cap > 0 && h[u] == h[edges_[i].v] + 1) {index = i;break;}}if (index != -1) {cur[u] = index;pre[edges_[index].v] = u;u = edges_[index].v;} else {if (--num[h[u]] == 0) {break;}int k = n;cur[u] = head_[u];for (int i = head_[u]; i != -1; i = edges_[i].next) {if (edges_[i].cap > 0 && h[edges_[i].v] < k) {k = h[edges_[i].v];}}if (k + 1 < n) {num[k + 1] += 1;}h[u] = k + 1;if (u != source) {u = pre[u];}}}return result;}MaxFlowSolver() = default;void Clear() {edges_.clear();head_.clear();vertex_indexer_.clear();used_index_ = 0;}void InsertEdge(int from, int to, FlowType cap) {from = GetIndex(from);to = GetIndex(to);AddEdge(from, to, cap);AddEdge(to, from, kZeroFlow);}private:int GetIndex(int idx) {auto iter = vertex_indexer_.find(idx);if (iter != vertex_indexer_.end()) {return iter->second;}int map_idx = used_index_++;head_.push_back(-1);return vertex_indexer_[idx] = map_idx;}void AddEdge(int from, int to, FlowType cap) {node p;p.v = to;p.cap = cap;p.next = head_[from];head_[from] = static_cast<int>(edges_.size());edges_.emplace_back(p);}std::vector<node> edges_;std::vector<int> head_;std::unordered_map<int, int> vertex_indexer_;int used_index_ = 0;
};class BlockTheBlockPuzzle {static constexpr int kInfinite = 1000000;public:int minimumHoles(const std::vector<std::string> &S) {MaxFlowSolver<int> solver;int n = static_cast<int>(S.size());int source = -1;int sink = -2;int tx = -1, ty = -1;for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {if (S[i][j] == '$') {tx = i;ty = j;}}}auto P0 = [&](int i, int j) { return i * n + j; };auto P1 = [&](int i, int j) { return i * n + j + n * n; };for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {if (i % 3 == tx % 3 && j % 3 == ty % 3) {if (S[i][j] == '$') {solver.InsertEdge(P1(i, j), sink, kInfinite);}if (S[i][j] == 'b') {solver.InsertEdge(source, P0(i, j), kInfinite);}if (S[i][j] != 'H') {solver.InsertEdge(P0(i, j), P1(i, j),S[i][j] == '.' ? 1 : kInfinite);}if (i + 3 < n) {auto cost = GetCost(S, i + 1, j, i + 2, j);solver.InsertEdge(P1(i, j), P0(i + 3, j), cost);solver.InsertEdge(P1(i + 3, j), P0(i, j), cost);}if (j + 3 < n) {auto cost = GetCost(S, i, j + 1, i, j + 2);solver.InsertEdge(P1(i, j), P0(i, j + 3), cost);solver.InsertEdge(P1(i, j + 3), P0(i, j), cost);}}}}auto result = solver.MaxFlow(source, sink);if (result >= kInfinite) {return -1;}return result;}private:int GetCost(const std::vector<std::string> &s, int x1, int y1, int x2,int y2) {if (s[x1][y1] == 'b' || s[x2][y2] == 'b') {return kInfinite;}int ans = 0;if (s[x1][y1] == '.') {++ans;}if (s[x2][y2] == '.') {++ans;}return ans;}
};

code for problem3

constexpr int kMod = 1000000007;
constexpr int kMax = 2000;int f[kMax + 1][kMax + 1];
int C[kMax + 1][kMax + 1];class Seatfriends {public:int countseatnumb(int N, int K, int G) {f[1][1] = N;for (int i = 1; i < K; ++i) {for (int j = 1; j <= G; ++j) {long long p = f[i][j];if (p == 0) {continue;}if (j < G) {(f[i + 1][j + 1] += static_cast<int>(p * j % kMod)) %= kMod;}(f[i + 1][j - 1] += static_cast<int>(p * j % kMod)) %= kMod;(f[i + 1][j] += static_cast<int>(p * 2 * j % kMod)) %= kMod;}}if (K == N) {return f[K][0];}C[0][0] = 1;for (int i = 1; i <= N; ++i) {C[i][0] = C[i][i] = 1;for (int j = 1; j < i; ++j)C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % kMod;}long long ans = 0;for (int j = 1; j <= G; ++j) {ans += static_cast<long long>(f[K][j]) * C[N - K - 1][j - 1] % kMod;}return static_cast<int>(ans % kMod);}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/537147.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring的组件赋值以及环境属性@PropertySource

PropertySource 将指定类路径下的.properties一些配置加载到Spring当中&#xff0c; 有个跟这个差不多的注解PropertySources Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented public interface PropertySources {PropertySource[] value();} 使用…

python语音识别框架_横评:五款免费开源的语音识别工具

编者按&#xff1a;本文原作者 Cindi Thompson&#xff0c;美国德克萨斯大学奥斯汀分校(University of Texas at Austin)计算机科学博士&#xff0c;数据科学咨询公司硅谷数据科学(Silicon Valley Data Science&#xff0c;SVDS)首席科学家&#xff0c;在机器学习、自然语言处理…

csharp read excel file get sheetName list

1 /// <summary>2 /// 3 /// 塗聚文4 /// 201208035 /// Geovin Du6 ///找到EXCEL的工作表名称 要考慮打開的文件的進程問題7 /// </summary>8 /// <param name"filename">…

Spring Bean的生命周期以及IOC源码解析

IOC源码这一块太多只能讲个大概吧&#xff0c;建议还是去买本Spring IOC源码解析的书来看比较好&#xff0c;我也是自己看源代码以及视频整理的笔记 Bean的生命周期大概可以分为四个阶段&#xff0c;具体的等会再说&#xff0c;先看看IOC的源码吧 1、bean的创建 2、bean的属…

python3绘图_python3绘图示例2(基于matplotlib:柱状图、分布图、三角图等)

#!/usr/bin/env python# -*- coding:utf-8 -*-from matplotlib import pyplot as pltimport numpy as npimport pylabimport os,sys,time,math,random# 图1-给已有的图加上刻度filer‘D:\jmeter\jmeter3.2\data\Oracle数据库基础.png‘arrnp.array(file.getdata()).reshape(fil…

bzoj4152-[AMPPZ2014]The_Captain

Description 给定平面上的n个点&#xff0c;定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|)&#xff0c;求从1号点走到n号点的最小费用。 Input 第一行包含一个正整数n(2<n<200000)&#xff0c;表示点数。 接下来n行&#xff0c;每行包含两个整数x[i],yi&#xff0c;…

python日志统计_python试用-日志统计

最近两天尝试用python代替bash写Linux Shell脚本来统计日志。发现python写起来比bash更简单和容易阅读&#xff0c;发现不少惊喜。所以写了一个粗糙的脚本来统计日志。目标1、通过简单命令和脚本统计事件发生数2、日志限定文本类型假定环境日志文件&#xff1a;1.logtest:aaa,1…

Spring AOP两种使用方式以及如何使用解析

AOP是一种面向切面编程思想&#xff0c;也是面向对象设计&#xff08;OOP&#xff09;的一种延伸。 在Spring实现AOP有两种实现方式&#xff0c;一种是采用JDK动态代理实现&#xff0c;另外一种就是采用CGLIB代理实现&#xff0c;Spring是如何实现的在上篇已经讲到了Spring Be…

如何用python生成可执行程序必须经过_python怎么生成可执行文件

.py文件&#xff1a;对于开源项目或62616964757a686964616fe58685e5aeb931333363393664者源码没那么重要的&#xff0c;直接提供源码&#xff0c;需要使用者自行安装Python并且安装依赖的各种库。(Python官方的各种安装包就是这样做的).pyc文件&#xff1a;有些公司或个人因为机…

Jmeter 老司机带你一小时学会Jmeter

Jmeter的安装 官网下载地址&#xff1a;http://jmeter.apache.org/download_jmeter.cgi 作为Java应用&#xff0c;是需要JDK环境的&#xff0c;因此需要下载安装JAVA&#xff0c;并且作必要的的环境变量配置。 一、bin目录 examples:    目录中有CSV样例 jmeter.bat/jmeter…

MongoDB位运算基本使用以及位运算应用场景

最近在公司业务上用到了二进制匹配数据&#xff0c;但是MongoDB进行二进制运算&#xff08;Bitwise&#xff09;没用过&#xff0c;网上博客文章少&#xff0c;所以就上官网看API&#xff0c;因此记录一下&#xff0c;顺便在普及一下使用二进制位运算的一些应用。 在MongoDB的…

好用的下拉第三方——nicespinner

1.简介 GitHub地址&#xff1a;https://github.com/arcadefire/nice-spinner Gradle中添加&#xff1a; allprojects {repositories {...maven { url "https://jitpack.io" }} }dependencies {implementation com.github.arcadefire:nice-spinner:1.3.7 }2.使用 xml文…

Mybatis配置文件参数定义

官网有时候进不去&#xff0c;所以就记录一下Mybatis的配置文件的各项参数定义&#xff0c;大家也可以上官网查询&#xff0c;官方文档&#xff0c;进不进的去看各自的缘分了 properties 定义配置&#xff0c;在这里配置的属性可以在整个配置文件使用&#xff1b;可以加载指定…

python和java后期发展_Python与java的发展前景谁最大

Python和Java是目前IT行业内两大编程语言&#xff0c;很多人都喜欢拿来比较&#xff0c;一个是后起之秀&#xff0c;潜力无限&#xff1b;一个是行业经典&#xff0c;成熟稳定。对于许多想从事IT行业的同学来说&#xff0c;这两门语言真的很难抉择。那么&#xff0c;Python和Ja…

JDK源码学习笔记——Enum枚举使用及原理

一、为什么使用枚举 什么时候应该使用枚举呢&#xff1f;每当需要一组固定的常量的时候&#xff0c;如一周的天数、一年四季等。或者是在我们编译前就知道其包含的所有值的集合。 利用 public final static 完全可以实现的功能&#xff0c;为什么要使用枚举&#xff1f; public…

Mybatis源码日志模块分析

看源码需要先下载源码&#xff0c;可以去Mybatis的github上的仓库进行下载&#xff0c;Mybatis 这次就先整理一下日志这一块的源码分析&#xff0c;这块相对来说比较简单而且这个模块是Mybatis的基础模块。 之前的文章有谈到过Java的日志实现&#xff0c;大家也可以参考一下&…

python手机端给电脑端发送数据_期货交易软件有哪些比较好用?分手机端和电脑端...

一、电脑端交易软件期货电脑端交易软件目前市场上用的最多的是文华财经和博易大师&#xff0c;这两个软件都是免费交易使用的。从投资者使用角度来看&#xff0c;目前电脑端文华财经的评价比博易大师高一些。当然每个投资者有自己的使用习惯&#xff0c;博易大师也有自己优点&a…

Find the Difference(leetcode389)

2019独角兽企业重金招聘Python工程师标准>>> Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in …

Mybatis源码之数据源模块分析

先来看看java纯jdbc查询数据的示例&#xff1a; try {//加载对应的驱动类Class.forName("com.mysql.cj.jdbc.Driver");//创建连接Connection connection DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?serverTimezoneUTC", "roo…

reactnative 获取定位_[RN] React Native 获取地理位置

import React, {Component} from react;import {StyleSheet, Text, View}from react-native;exportdefault classTestGeo extends Component {state{longitude:,//经度latitude: ,//纬度city: ,district:,street:,position:,//位置名称};componentWillMount () >{this.getPo…