![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
优秀算法总结
薄层
努力就有收获~
展开
-
HDU-3191 How Many Paths Are There(有向图次短路条数)
oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see原创 2017-07-14 17:25:06 · 371 阅读 · 0 评论 -
HDU-5950 Recursive sequence(矩阵乘法)
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say the原创 2017-10-18 18:53:18 · 311 阅读 · 0 评论 -
C-Cleaning Pipes(判断两线段相交+二分图判定) 2015-2016 Northwestern European Regional Contest (NWERC 2015)
Linköping has a quite complex water transport system.Around Linköping there are several wells from which wateris drawn. The water is then transported to other locationsusing pipes. Each pipe is a straight canal from one of thewells to some location in原创 2017-10-15 19:35:14 · 407 阅读 · 0 评论 -
HDU-5833 Zhu and 772002(异或方程高斯消元)
Zhu and 772002 are both good at math. One day, Zhu wants to test the ability of 772002, so he asks 772002 to solve a math problem. But 772002 has a appointment with his girl friend. So 772002 gives this problem to you. There are nn numbers a1,a2,...,ana1原创 2017-10-04 15:49:23 · 268 阅读 · 0 评论 -
HDU-6129 Just do it - 2017 Multi-University Training Contest - Team 7(规律、杨辉三角、组合数奇偶性)
There is a nonnegative integer sequence a1...na1...n of length nn. HazelFan wants to do a type of transformation called prefix-XOR, which means a1...na1...n changes into b1...nb1...n, where bibi equals to the XOR value of a1,...,aia1,...,ai. He will repeat原创 2017-08-16 21:01:39 · 366 阅读 · 0 评论 -
HDU-4686 Arc of Dream(推公式+矩阵快速幂)
An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0 b i = b i-1*BX+BY What is the value of AoD(N) modulo 1,000,000,007?原创 2017-07-30 20:15:13 · 334 阅读 · 0 评论 -
ZJC-1489 L先生与质数V4 51Nod-1184 第N个素数 (大区间求素数个数模板+二分)
Description在解决了上一个质数问题之后,L先生依然不甘心,他还想计算下更多范围内的质数,你能帮助他吗?(没错这题题面和V3一毛一样)Input有多组测试例。(测试例数量<70)每个测试例一行,输入一个数字n(0<n<=3000000),输入0表示结束。Output输出测试例编号和第N个质数。Case X: YSample Input1234101000Sample OutputCase 1: 2Case 2: 3Case 3: 5Case 4: 7Case原创 2017-07-11 01:19:44 · 517 阅读 · 0 评论 -
Meisell-Lehmer算法(求1...n范围内的素数个数)
求1...n范围内的素数个数有多少了,如果数据不大的话素筛肯定就可以了,但像HDU-5901中n的范围为10^11肯定就不能素筛了,所以这里介绍Meisell-Lehmer算法,代码颇是复杂,摘的网上的模板然后改成自己的代码习惯。//Meisell-Lehmer算法#include #include #define LL long longusing namespace st原创 2017-05-15 00:38:19 · 1458 阅读 · 0 评论 -
关于数论乘法逆元及相关知识点
在求解a/b%m时,可以转化为(a%(b*m))/b,转化过程如下令k = (a/b)/m(向下取整), x = (a/b)%m;a/b = k*m + x (x a = k*b*m + b*x;a%(b*m) = b*x;a%(b*m)/b = x;得证: a/b%m = a%(b*m)/b;所有的除法取模问题都可以用这种方法,但是当b很大的时候,则会出现爆精度问题,所以引出乘法逆元,将除法取模转换为乘法取模。b存在乘法逆元的充要条件是b与模数m互质。设令c为b的逆元,即b*c ≡ 1(原创 2017-05-10 20:57:47 · 9674 阅读 · 1 评论 -
UOJ-79 一般图的最大匹配(带花树模板求解)
#79. 一般图最大匹配从前一个和谐的班级,所有人都是搞OI的。有 nn 个是男生,有 00 个是女生。男生编号分别为 1,…,n1,…,n。现在老师想把他们分成若干个两人小组写动态仙人掌,一个人负责搬砖另一个人负责吐槽。每个人至多属于一个小组。有若干个这样的条件:第 vv 个男生和第 uu 个男生愿意组成小组。请问这个班级里最多产生多少个小组?输入格式原创 2017-10-09 17:19:26 · 356 阅读 · 0 评论 -
POJ-1330 Nearest Common Ancestors(lca模板题)
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an原创 2017-09-15 12:55:14 · 299 阅读 · 1 评论 -
HDU-1043 Eight(经典八数码问题, A*+康拓+曼哈顿距离+逆序数判断可解性、双向搜索)
八数码经典问题The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let'原创 2017-10-12 15:58:49 · 553 阅读 · 0 评论 -
POJ-3255 Roadblocks(次短路)
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the原创 2017-07-13 23:45:36 · 579 阅读 · 0 评论 -
HDU-1688 Sightseeing(最短路+次短路)
Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a n原创 2017-07-13 23:06:54 · 484 阅读 · 0 评论 -
Floyd算法求无向图最小环
There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is neces原创 2017-07-13 01:19:02 · 6454 阅读 · 0 评论 -
POJ-2449 Remmarguts' Date(A*算法+SPFA求第K短路)
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring coun原创 2017-07-12 00:25:16 · 571 阅读 · 0 评论 -
HDU-5876 & SCU-4444 (完全图的最短路)
完全图求最短路(各边权值必须得相同),n稍大点边肯定就存不下了,这儿有一种bfs的做法:维护两个集合,st1代表当前点能够到达的所有点的集合,和st2代表不能到达的所有点的集合。BFS搜索,选取当前点,然后将所有与该点相连的点从st1中删去,并添加到st2。然后再拓展st1中的点,将之入队,并将st1和st2两个集合进行交换(因为已经拓展完的肯定就是最短的了嘛,只需要拓展没到达的)。原创 2017-06-18 18:18:21 · 1083 阅读 · 0 评论 -
计蒜客复赛D题 百度地图导航
百度地图上有 nn 个城市,城市编号依次为 11 到 nn。地图中有若干个城市群,编号依次为 11 到 mm。每个城市群包含一个或多个城市;每个城市可能属于多个城市群,也可能不属于任何城市群。地图中有两类道路。第一类道路是 城市之间的快速路,两个城市 u,vu,v 之间增加一条距离为 cc 的边;第二类道路是城市群之间的高速路,连接两个城市群 a,ba,b,通过这条高速路,城市群 aa 里的每原创 2017-06-10 21:32:30 · 578 阅读 · 0 评论 -
二分图的概念汇总
二分图概念汇总--二分图:是这样一个图,其顶点可分为两集合X和Y,所有的边关联的两顶点中,恰一个属于X,另一个属于Y。同一集合的结点不相邻。匹配:图的一个匹配是一些边的集合,任意两条边没有公共点。最大匹配:包含边数最多的匹配。 (匈牙利算法)完美匹配:所有点都在匹配边上的匹配。完备匹配:在二分图中,X集合中的所有点都有对应的匹配或者是Y集合中的所有点都有原创 2017-05-21 00:43:14 · 638 阅读 · 0 评论 -
KM算法模板(二分图的最大权匹配)
又掌握了一个模板~~~其实是差不多理解了,比较菜..二次元霸主写博客就是强啊: http://www.cnblogs.com/wenruo/p/5264235.html但...不敢想象博主是男是女。依旧改成自己喜欢的模板格式~//模板HDU-2255 #include #include #include #include using namespace std;原创 2017-05-16 15:38:55 · 568 阅读 · 0 评论 -
DLX模板之精确覆盖和重复覆盖
给定一个n*m的矩阵,有些位置为1,有些位置为0。如果G[i][j]==1则说明i行可以覆盖j列。Problem:1)选定最少的行,使得每列有且仅有一个1.2)选定最少的行,使得每列至少一个1.原创 2017-10-03 15:33:11 · 898 阅读 · 0 评论 -
HDU-5115 Dire Wolf(区间dp)
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor.Dire wolves look like normal wolves, but these creatures are of nearly twice the size. These powerful bea原创 2017-07-12 16:55:28 · 379 阅读 · 0 评论 -
LeetCode 202 Happy Number(floyd判圈算法(龟兔赛跑算法))
题目链接:https://leetcode.com/problems/happy-number/description/Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with原创 2017-10-20 20:10:29 · 644 阅读 · 0 评论 -
POJ-2689 Prime Distance(区间素数筛--经典题)
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper fac原创 2017-08-04 16:31:55 · 792 阅读 · 0 评论 -
HDU-6069 Counting Divisors - 2017 Multi-University Training Contest - Team 4(分解质因子区间筛法)
In mathematics, the function d(n)d(n) denotes the number of divisors of positive integer nn. For example, d(12)=6d(12)=6 because 1,2,3,4,6,121,2,3,4,6,12are all 1212's divisors. In this problem, given l,rl,r and kk, your task is to calculate the following原创 2017-08-04 16:50:33 · 332 阅读 · 0 评论 -
POJ-2823 && POJ-3250 (单调队列 && 单调栈)
poj-2823: 求n个数序列中的所有段长度为k的区间内的最大值和最小值。poj-3250: 求n个数序列中每个位置的右边的数小于其数值的个数,并且中间不能有大于等于它的数。poj-2823 下面这份代码只能在c++编译器下才能过,其它编译器超时,其实只需要在出队的时候加个二分优化就能过,但懒得改了,主要是学单调队列的做法。#include using namespace原创 2017-10-11 12:01:52 · 257 阅读 · 0 评论 -
HDU-6214 Smallest Minimum Cut(最小割)
2017 ACM/ICPC Asia Regional Qingdao OnlineConsider a network $G=(V,E)$ with source $s$ and sink $t$. An s-t cut is a partition of nodes set $V$ into two parts such that $s$ and $t$ belong to different parts. The cut set is the subset of $E$ with all edges原创 2017-09-18 13:24:32 · 701 阅读 · 5 评论 -
QDU 礼尚往来(组合数学之错排公式)
每当节日来临,女友众多的xxx总是能从全国各地的女友那里收到各种礼物。有礼物收到当然值得高兴,但回礼确是件麻烦的事!无论多麻烦,总不好意思收礼而不回礼,那也不是xxx的风格。 现在,即爱面子又抠门的xxx想出了一个绝妙的好办法:他准备将各个女友送来的礼物合理分配,再回送不同女友,这样就不用再花钱买礼物了! 假设xxx的n个女友每人送他一个礼物(每个人送的礼物都不相同),现在他需要合理安排,再回送每个女友一份礼物,重点是,回送的礼物不能是这个女友之前送他的那个礼物,不然,xxx可就原创 2017-06-27 19:21:01 · 480 阅读 · 0 评论 -
POJ-3352 Road Construction(边双连通分量+缩点)
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead betwe原创 2017-08-05 16:33:32 · 405 阅读 · 0 评论 -
HDU-6184 (无向图三元环计数)
Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting stars now! There are n stars in the sky, and little A has connected them by m non-directional edges. It is guranteed that no edges connect one star with it原创 2017-09-06 17:19:53 · 1897 阅读 · 1 评论 -
CodeForces-835E The penguin's game(思维+二进制乱搞+交互题)
Pay attention: this problem is interactive.Penguin Xoriy came up with a new game recently. He has n icicles numbered from 1 to n. Each icicle has a temperature — an integer from 1 to 109. Exactly two of these icicles are special: their temperature is y, w原创 2017-08-31 14:14:27 · 336 阅读 · 0 评论 -
UESTC-1006 最长上升子序列(最长递减子序列做法+贪心策略)
一个数的序列B=(b1,b2,⋯,bS)B=(b1,b2,⋯,bS),当b1<b2<⋯<bSb1<b2<⋯<bS 的时候,我们称这个序列是上升的。对于给定的一个序列A=(a1,a2,⋯,aN)A=(a1,a2,⋯,aN),我们可以得到一些上升的子序列(ai1,ai2,⋯,aiK)(ai1,ai2,⋯,aiK),这里1≤i1<i2<⋯<iK≤N1≤i1<i2<⋯<iK≤N。比如,对于序列(1,7,3,5,9,4,8)(1,7,3,5,9,4,8),有它的一些上升子序列,如(1,7)(1,7), (3,4,8)原创 2017-07-09 23:13:44 · 831 阅读 · 0 评论 -
美团CodeM初赛B轮-黑白树(树形结构贪心)
一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1。树上每个节点i对应一个值k[i]。每个点都有一个颜色,初始的时候所有点都是白色的。你需要通过一系列操作使得最终每个点变成黑色。每次操作需要选择一个节点i,i必须是白色的,然后i到根的链上(包括节点i与根)所有与节点i距离小于k[i]的点都会变黑,已经是黑的点保持为黑。问最少使用几次操作能把整棵树变黑。原创 2017-07-09 23:31:00 · 471 阅读 · 0 评论 -
HYSBZ-3229 石子合并(GarsiaWachs算法优化)
现在有n堆石子,第i堆有ai个石子。现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数。求合并所有石子的最小代价。对于 100% 的数据,1≤N≤40000; 对于 100% 的数据,1≤A≤200原创 2017-07-08 20:25:37 · 903 阅读 · 0 评论 -
CSU-1592 石子归并(区间dp)
题意:现在有n堆石子,第i堆有ai个石子。现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数。求合并所有石子的最小代价。思路:入门博文讲的很详细,下面看自己的代码细节。Code:#include #include #include #include #define LL long longusing namespace std;co原创 2017-07-08 16:45:32 · 299 阅读 · 0 评论 -
HDU 3037(隔板法+组合数+Lucas)
题意:求在n棵树上摘不超过m颗豆子的方案数,结果对p取膜。思路:其实就相当于把i(0 很明显,需要用到隔板法。所以对于i个球,方案数为C(i+n-1, n-1)。总方案数为C(n-1, n-1)+C(n, n-1)+...+C(n+m-1, n-1);然后根据公式C(n, m) = C(n, n-m)得方案数为C(n-1, 0)+C(n, 1)+...+C(n+m-1, m);原创 2017-06-23 17:19:53 · 416 阅读 · 0 评论 -
LightOJ-1253 Misere Nim(Nim求解不正常的博弈)
Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, each pile containing one or more stones. The players alternate turns and in each turn a player can select one of the piles and can remove as many stones from t原创 2017-10-21 21:20:34 · 616 阅读 · 0 评论 -
CodeForces-731E Funny Game(DP+Games)
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't st原创 2017-08-13 12:30:36 · 369 阅读 · 0 评论 -
HDU-6166 Senior Pan - 2017 Multi-University Training Contest - Team 9(最短路+二进制乱搞)
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday. The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum d原创 2017-09-01 16:48:11 · 427 阅读 · 0 评论 -
HDU-4886(hash+暴力枚举)
题意:给一个主串s(只包括‘A’‘B’‘C’‘D’‘E’‘F’‘G’‘H’),然后要找出一个串ans(也只包括‘A’‘B’‘C’‘D’‘E’‘F’‘G’‘H’),ans满足条件:在s所有子串中没出现过,其次保证长度最短,最后保证字典序最小。思路:可以估计ans的长度最长为7,因为要使主串s中存在所有的8个字符的排列需要长度为8^7,已经超过了题目给定长度。枚举所有长度的子串,然后将字符串哈希成原创 2017-06-21 12:39:39 · 434 阅读 · 0 评论