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.pswp.cn/news/537147.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/537147.shtml
英文地址,請注明出處:http://en.pswp.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…