begin

在次线性时间内计算线性递归数列

最后都变了- 提交于 2019-12-11 00:07:48
date: 2019-09-13 简介 标题看着很高端,其实就是在 \(O(\log_2 N)\) 内计算出线性递归数列的某一项 (好像什么都没有解释清楚啊) 。 斐波那契数列的快速计算 我们先来看一个题目: 题目描述: ​ 给你一个数字 \(n\) ,你需要输出斐波那契数列的第n项。 ​ 注意: 第一项和第二项都为 \(1\) 。 题目输入: ​ 第一行一个整数 \(n\) ,保证$1 \leq n \leq 1e18 $。 题目输出: ​ 输出斐波那契数列的第n项,对 \(1e9+7\) 取模。 这道题目小数据范围内很简单 (一道水题) ,但是当数据范围扩展到 \(1 \leq n \leq 1e18\) 时,一切都不一样了。你需要一个 \(O(N \log_2 N)\) 的算法来解决它。这里我们需要用到矩阵运算。 递推式 斐波那契数列的递推式这个大家应该都会写吧。。。 算了我还是来写一下好了: \[ f_{i}=f_{i-1}+f_{i-2}\\ f_{0}=1\\ f_{1}=1 \] 转化为矩阵计算 这里我们把计算 \(f_0\) 和 \(f_1\) 包含在一个2行1列的矩阵中: \[ \begin{pmatrix} f_{0}\\ f_{1} \end{pmatrix} \] 之后我们可以得出如下的式子: \[ \begin{pmatrix} 0&1\\ 1&1

【Python CheckiO 题解】Between Markers

不问归期 提交于 2019-12-10 23:41:36
CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,传送门:https://checkio.org/,本博客主要记录自己在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。 题目描述 【Between Markers】 :给定一个字符串和两个标记字符(第一个和最后一个标记),找到两个标记符之间包含的子字符串。初始标记和最终标记始终不同;如果这两个标记在字符串中都不存在,则返回原字符串;如果没有初始标记,则应将第一个字符视为字符串的开头;如果没有最终标记,则最后一个字符应视为字符串的结尾;如果最终标记位于初始标记之前,则返回一个空字符串。 【链接】 :https://py.checkio.org/zh-hans/mission/between-markers/ 【输入】 :三个参数,都是字符串,第二个和第三个参数是初始标记和结束标记 【输出】 :字符串 【范例】 : between_markers ( 'What is >apple<' , '>' , '<' ) == 'apple' between_markers ( 'No[/b] hi' , '[b]' , '[/b]' ) == 'No' 解题思路 利用 find() 方法查找原字符串中是否有标记字符,注意,由于

1037 Magic Coupon (25分)

我的梦境 提交于 2019-12-10 15:13:17
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N's! For example, given a set of coupons { 1 2 4 − }, and a set of product values { 7 6 − − } (in Mars dollars M$) where a

awk学习笔记

假装没事ソ 提交于 2019-12-10 06:25:23
awk的工作流程: 将文件的一行读到内存,然后按分隔符将该行进行分段,其默认的分隔符是空格和TAB。 常用格式1: awk /pattern/ {command1;command2.....} file 用法1 指定分隔符 用-F'分隔符' awk -F: '{print $1}' /etc/passwd 用法2 指定匹配位置用$n ~ awk -F: '$5 ~ /root/ {print $2}' /etc/passwd 常用格式2: awk 'BEGIN{command}/pattern/{command1;command2....}' 用法3 用BEGIN{command}来指定分隔符(FS为AWK内置变量,表示分隔符) awk 'BEGIN{FS=":"}$5 ~ /deamon/ {print $2}' /etc/passwd 用法4 用OFS变量设置输出分隔符(默认为空格) awk 'BEGIN{FS=":";OFS="--"}$5 ~ /daemon/ {print $1,$2}' passwd.log 用法5 NF变量示例(NF表示每行的字段数) awk 'BEGIN{FS=":"} {print NF}' passwd.log 或者awk -F: '{print NF}' passwd.log 用法6 NR变量(处理行在原文件中的行号) awk '/^adm/

awk命令详解

落花浮王杯 提交于 2019-12-10 05:28:14
转: awk命令详解 简单使用: awk :对于文件中一行行的独处来执行操作 。 awk -F :'{print $1,$4}' :使用‘:’来分割这一行,把这一行的第一第四个域打印出来 。 详细介绍: AWK 命令介绍 awk语言的最基本功能是在文件或字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作,完整的awk脚本通常用来格式化文本文件中的信息 1. 调用awk: 第一种命令行方式,如: awk [-Field-separator] 'commands' input-file(s) 这里commands是真正的awk命令,[-F域分隔符]是可选的,awk默认使用空格分隔,因此如果要浏览域间有空格的文本,不必指定这个选项,但如果浏览如passwd文件,此文件各域使用冒号作为分隔符,则必须使用-F选项: awk -F : 'commands' input-file 第二种,将所有awk命令插入一个文件,并使awk程序可执行,然后用awk命令解释器作为脚本的首行,以便通过键入脚本名称来调用它 第三种,将所有awk命令插入一个单独文件,然后调用,如: awk -f awk-script-file input-file -f选项指明在文件awk-script-file的awk脚本,input-file是使用awk进行浏览的文件名 2. awk脚本:

[JSTL]标签的使用

早过忘川 提交于 2019-12-10 05:02:56
在 JSP 的开发中,迭代是经常要使用到的操作。例如,逐行的显示查询的结果等。在早期的 JSP 中,通常使用 Scriptlets 来实现 Iterator 或者 Enumeration 对象的迭代输出。现在,通过 JSTL 的迭代标签可以在很大的程度上简化迭代操作。 JSTL 所支持的迭代标签有两个,分别是 <c:forEach> 和 <c:forTokens> 。在这里介绍的是 <c:forEach> 标签。 简单点说, <c:forEach> 标签的作用就是迭代输出标签内部的内容。它既可以进行固定次数的迭代输出,也可以依据集合中对象的个数来决定迭代的次数。 <c:forEach> 标签的语法定义如下所示。 <c:forEach var="name" items="expression" varStatus="name" begin="expression" end="expression" step="expression"> body content </c:forEach> <c:forEach> 标签具有以下一些属性: l var :迭代参数的名称。在迭代体中可以使用的变量的名称,用来表示每一个迭代变量。类型为 String 。 l items :要进行迭代的集合。对于它所支持的类型将在下面进行讲解。 l varStatus :迭代变量的名称,用来表示迭代的状态

What is a lambda expression in C++11?

偶尔善良 提交于 2019-12-09 22:03:29
The problem C++ includes useful generic functions like std::for_each and std::transform , which can be very handy. Unfortunately they can also be quite cumbersome to use, particularly if the functor you would like to apply is unique to the particular function. #include < algorithm > #include < vector > namespace { struct f { void operator ()( int ) { // do something } }; } void func(std::vector < int >& v) { f f; std::for_each(v.begin(), v.end(), f); } If you only use f once and in that specific place it seems overkill to be writing a whole class just to do something trivial and one off. In C+

基本查找算法

…衆ロ難τιáo~ 提交于 2019-12-09 20:13:29
顺序查找: 时间复杂度 O(n); 步骤描述: 循环 n 次,将每一个值与要查找的值作比较 代码实现: Public int search(int[] array,int num){ For(int i=0;i<num;i++){ If(array[i]==num){ return i+1; } } Return -1; } 二分查找 : 时间复杂度: O(log2n) 步骤描述: 咋限定条件下,递归的找到数组最中间的值 middle ,比较和要查找的值的大小。根据大小递归调用,如果相等则查找成功; 代码实现: Int array[]; Int num; Public int Search(int[] array,int begin,int end){ If(num>array[end]||num<array[begin]||begin>end){ Return -1; } Int middle=(begin+end)/2; If(num>array[middle]){ Search(array,middle+1,end); Return; }else if(num<array[middle]){ Search(array,begin,middle-1); Return; } Return middle; } 插值查找: 在二分查找的基础上,每次查找条件不是从中间平分

在C++中实现foreach循环,比for_each更简洁!

霸气de小男生 提交于 2019-12-09 05:50:44
python,c#,java里面都有类似于foreach的结构,stl里面虽然有for_each这个函数,但是感觉使用还是太繁琐了一些,所以就自己实现了一个。 先来看看stl里面的for_each函数,官方文档上的原型如下: 1 Function for_each ( InputIterator first , InputIterator last , Function f ) ; 示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // for_each example #include <iostream> #include <algorithm> #include <vector> using namespace std ; void myfunction ( int i ) { cout << " " << i ; } struct myclass { void operator ( ) ( int i ) { cout << " " << i ; } } myobject ; int main ( ) { vector < int > myvector ; myvector . push_back ( 10 ) ; myvector .

基本排序算法

烂漫一生 提交于 2019-12-08 08:49:31
排序算法: 快速排序: 时间复杂度 O(nlogn) 空间复杂度 1 适用于大多数排序,性能很高 不稳定排序 步骤描述: 取数组首元素为基准值。设置一个 i 指针指向首元素,再设置一个 j 指针指向尾元素。在 i<j 的前提下。从 j 开始往回找,遇到第一个比基准值小的数,将该元素与基准值交换, i++ ;再从 i 往后找,遇到第一个比基准值大的值,使它与基准值交换, j++ ;直到 i>=j 停止。 代码: int length=array.length; public void sort(int[] array,int begin,int end){ int i=begin; int j=end; int base=array[begin]; While(i<j){ Int temp; While(i<j&&array[j]>base){ j--; } if(i<j){ temp=array[j] array[i]=array[j]; array[j]=temp; i++; } While(i<j&&array[i]<base){ i++; } If(i<j){ temp=array[i]; array[i]=array[j]; array[j]=temp; j--; } } array[i]=base; sort(array,begin,i-1); Sort(array,i