![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
程序设计算法
挖坑我第一
kylinholmes
这个作者很懒,什么都没留下…
展开
-
蓝桥杯算法训练 素因子去重
蓝桥杯算法训练 素因子去重这个题不难,是我对书理解不透彻,把一个习题经验用错了地方,故记录成博客,告诫自己。错误之处:int pri[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};在《算法进阶指南》里,约数那一部分提过一个引理:1~N中任何数的不同质因子都不会超过 10 个,且所有质因子的指数总和不超过30。证明:最小的11个质数的乘积2×3×5×7×11×13×19×23×29×31>2×1092\times3\times5\times7原创 2020-10-06 22:36:34 · 176 阅读 · 2 评论 -
货仓选址问题
货仓选址题目描述在一条数轴上有N家商店,它们的坐标分别为 A[1]~A[N]。现在需要在数轴上建立一家货仓,每天清晨,从货仓到每家商店都要运送一车商品。为了提高效率,求把货仓建在何处,可以使得货仓到每家商店的距离之和最小。输入格式第一行一个整数N,第二行N个整数A[1]~A[N]。输出格式一个整数,表示距离之和的最小值。样例input46 2 9 1output12数据范围与约定对于100%的数据: N<=100000, A[i]<=1000000题目很简单原创 2020-09-22 14:29:39 · 1133 阅读 · 0 评论 -
博弈论的算法总结
常见博弈算法模型总结转载自大佬博客文章目录常见博弈算法模型总结博弈:常见的博弈:巴什博弈:威佐夫博弈:尼姆博弈(Nimm Game):博弈:博弈论又被称为对策论(Game Theory),既是现代数学的一个新分支,也是运筹学的一个重要学科。博弈,具体的例子就是下棋,双方都考虑最有利于自已的步骤,但是最终必有一方输,一方赢。博弈的策略:参与者在行动之前所准备好的一套完整的行动方案,就是想好下完这步棋,对方会如何下,以及接下来该如何下,最终得出结果。常见的博弈:博弈:合作博弈和非合作博弈转载 2020-08-13 15:23:54 · 9387 阅读 · 1 评论 -
Who is better?
Who is better?题目看懂了就很简单,首先是给你a和b,看能不能找到这样一个n满足上述关系。用中国剩余定理来求。如果找不到就是"Tankernb!"找到的话就用 斐波那契博弈论 的结论。石头数 为 斐波那契数的话 先手 必败。思路是先算n,然后计算斐波那契数。#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include原创 2020-08-05 17:18:49 · 153 阅读 · 2 评论 -
中国剩余定理模版
#define ll long longll n,i,fail;ll a,b,lcm,now,k,d,x,y;ll exgcd(ll a,ll b,ll &x,ll &y){ if(!b) {x=1; y=0; return a;} LL c=exgcd(b,a%b,x,y),z=x; x=y; y=z-y*(a/b); return c;}ll CRT(int n){ //n 表示n个方程组 scanf("%lld%lld",&原创 2020-08-05 16:48:33 · 82 阅读 · 2 评论 -
离散化最妙写法
离散化最妙的写法 map<int,int> m; for(int i=0;i<n;i++){ scanf("%d",&x); x = m[x] ? m[x] : (m[x] = ++tot); }原创 2020-07-12 11:36:23 · 185 阅读 · 0 评论 -
来看排列组合嘛
组合数的逆元写法看别人的没看明白,顺着自己思路写了一下其实也没什么好讲的,就是公式的写法,打表了 n!,还有对应的逆元inv。组合的公式: Cnm=n!m!(n−m)!C^m_n = \frac{n!}{ m!(n-m)!}Cnm=m!(n−m)!n!之后把分母换成对应的inv就行了问题: 给定n,m 求CnmC^m_nCnm#include <iostream>#define ll long longusing namespace std;const int N = 1原创 2020-06-28 01:37:17 · 176 阅读 · 0 评论 -
算法竞赛进阶指南练习 0x18
POJ 2823 Sliding Window这个题大方向想到了,但是没想到要删除队尾的元素来维护单调性。后来测试数据的时候发现之前的写法有问题,想了一圈不知道应该改哪里,后来还是去搜了一下别人的思路,发现需要维护队尾的单调性。最后借鉴的标答的代码,写的真的简洁,比其他人的都短好多。除了单调队列,这个是不是还可以用区间dp,线段树。但我觉得可能会超时吧。#include <iostream>#include <cstring>#include <algorithm原创 2020-06-23 20:52:25 · 255 阅读 · 0 评论 -
二分图的匹配问题 以及 相关算法模板
二分图的匹配问题以及相关 算法模板二分图的判定 染色法染色法原理:首先任意取出一个顶点进行染色,和该节点相邻的点有三种情况:1.如果未染色,那么继续染色此节点(染为另一种颜色)2.如果已染色但和当前节点颜色不同,则跳过该点3.如果已染色并且和当前节点颜色相同,返回失败(该图不是二分图)明确二分图、奇数环、染色法之间的关系:如果一个图中存在奇数环,那么这个图一定不是二分图;这一点显...原创 2020-06-19 15:48:48 · 139 阅读 · 0 评论 -
算法竞赛进阶指南练习 0x08
The Pilots Brothers’ refrigerator占卜DIY思路很简单,模拟一遍就行,但是我自己写的太丑了,看懂别人的以后重写了一遍,真的妙啊,人都升华了。#include <iostream>#include <cstring>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace原创 2020-06-14 19:01:04 · 508 阅读 · 0 评论 -
CodeForces EDU 89 div.2 D
CodeForces EDU 89 div.2 D这个题其实不难,想一下就容易想到了,但是我没想到????巨佬的原文const int N = 5e5+5;const int M = 1e7+7;int n;int a[N],b[N],c[N],L=0;int v[M];void prime(int n){ for(int i=2;i<=n;i++){ if(v[i]==1)continue; for(int j=i*2;j<=n;j+=i原创 2020-06-12 17:35:43 · 175 阅读 · 0 评论 -
CF Round648 div2 C Rotation Matching
CF Round648 div2 C代码太妙了,我直接复制了别人的来因为转载要链接,就发原创了,但是代码不是我写的ans是第几圈,ji很好看出来是位置#include <cstdio>#include <iostream>#include <iomanip>#include <string>#include <cstdlib>#include <cstring>#include <queue>#incl原创 2020-06-08 00:03:10 · 223 阅读 · 0 评论 -
高精度算法
高精度算法高精度加法求a + b#include <iostream>#include <cstring>using namespace std;const int N = 1e6+6;int a[N],b[N],c[N];char s[N];int L;int la,lb;void add(int *a,int *b){ int t; for(int i=0;i<la||i<lb;i++){ t = a[i]原创 2020-06-05 08:43:17 · 271 阅读 · 0 评论 -
树桩数组模板
树桩数组模板#define lowbit(x) (x)&(-x) //查找父节点const int N = 1e6+6;int bit[N],n;// 减法的时候就 +(-x),单点修改void add(int i,int x){ while(i <= n){ bit[i]+=x; i=lowbit(i); }}// sum是1~i的和// sum[l,r] = sum(r) - sum(l-1)int sum(int i){ int s = 0; while原创 2020-05-13 01:45:44 · 237 阅读 · 2 评论 -
排序模板
排序模板快排void q_sort(int q[],int l,int r){ if(l >= r) return; int i = l - 1,j = r + 1,x = q[(l + r) >> 1]; while(i<j){ do i++; while(q[i]<x); do j++ ;while(q[j]>x); if(i < j) swap(q[i],q[j]); }原创 2020-05-10 08:22:14 · 107 阅读 · 0 评论 -
POJ 2105 IP Address (水题)
POJ 2105 IP Address (水题)IP AddressTime Limit: 1000MSMemory Limit: 30000KDescriptionSuppose you are reading byte streams from any device, representing IP addresses. Your task is to convert ...原创 2020-04-24 21:30:29 · 257 阅读 · 0 评论 -
POJ 2102 Honey and Milk Land (水题)
POJ 2102 Honey and Milk Land (水题)Honey and Milk LandTime Limit: 1000MSMemory Limit: 64000KDescriptionBad rumors are spreading over the Land of Honey and Milk. Informed people say that the ...原创 2020-04-24 18:45:18 · 180 阅读 · 0 评论 -
POJ 2100 Graveyard Design (尺取法)
Graveyard DesignTime Limit: 10000MS Memory Limit: 64000K Total Submissions: 12120 Accepted: 3055 Case Time Limit: 2000MS DescriptionKing George has recently decided that he w...原创 2020-04-24 17:48:48 · 159 阅读 · 1 评论 -
POJ 2081 WaterProblem(水题)
POJ2081:Recaman’s SequenceDescriptionThe Recaman’s sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence, otherwise am = am−1 + m...原创 2020-04-20 16:35:12 · 182 阅读 · 0 评论 -
POJ 2070 Water Problem(水题)
POJ 2070 Water Problem(水题)题意是给你一组数据,在已知的三个人里找到满足条件的输出出来,中间加空格。有多个就输出多个。条件就是,看数据嘛,满足第一个人那个关系的就是了。//// main.cpp// Problem//// Copyright © 2020 Kylin. All rights reserved.#include <iostrea...原创 2020-04-19 18:14:18 · 139 阅读 · 0 评论 -
凸包 Graham扫描算法 模板
凸包 Graham扫描算法 模板struct P{ int x,y; // ? 构造函数,dot,重载 - 运算符}bool cmp(const P& p,const P& q){ if(p.x != q.x) return p.x < q.x; return p.y < q.y;}vector<P> convex_hull(P * ps...原创 2020-04-19 17:41:14 · 157 阅读 · 0 评论 -
POJ 2063 投资 (完全背包水题)
POJ 2063 投资 (完全背包水题)DescriptionJohn never knew he had a grand-uncle, until he received the notary’s letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-Ameri...原创 2020-04-18 23:27:43 · 248 阅读 · 0 评论 -
POJ 2069 Super Start (最小球覆盖,退火算法)
POJ 2069 Super Start (最小球覆盖,退火算法)退火法主要参考这篇文章DescriptionDuring a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard the...原创 2020-04-18 18:11:36 · 245 阅读 · 1 评论 -
Eratosthenes 埃式素数筛 模板
Eratosthenes 埃式素数筛埃式素数筛的思路是这样的: 任何整数 x 的倍数 2x,3x…一定都不是素数。我们从小到大开始扫描,每扫描到一个数就标记为合数。当扫描到一个数时,没有被标记为合数,那么说明在2 ~ x - 1之间没有x的因数,所以 x 就是质数。const int N = 1e5+6;bool v[N];void prime(int n){ memset...原创 2020-04-17 20:00:28 · 145 阅读 · 0 评论 -
POJ 2084 连接游戏 (卡特兰数)
POJ 2084 连接游戏 (卡特兰数)DescriptionThis is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circl...原创 2020-04-17 17:16:52 · 346 阅读 · 0 评论 -
p1004方格取数
p1004方格取数题目描述设有 N×N 的方格图 (N≤9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 0。如下图所示(见样例):A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0 14 0 0 0 0 0 21 0 0 0 4 0 0 ...原创 2020-04-16 15:46:40 · 172 阅读 · 0 评论 -
背包问题 - 01背包 、完全背包 、多重背包、还有混合背包
背包问题主要讲三个背包:01 背包,完全背包,多重背包。01 背包01背包的意思是选一个或者不选。朴素版:#include <iostream>#include <algorithm>using namespace std;const int N = 1111;int v[N]; //v[i]表示i个物品的价值int w[N]; //w[i]表示第...原创 2020-04-15 03:07:05 · 227 阅读 · 1 评论