[Data Structures and Algorithms - 1] Introduction & Mathematics
References:
1. Stanford University CS97SI by Jaehyun Park
3. Kuangbin's ACM Template
4. Data Structures by Dayou Liu
Getting Started:
1) What is a good algorithm?
The answer could be about correctness, time complexity, space complexity, readability, robustness, reusability, flexibility, etc.
However, in competitive programming, we care more about
- Correctness - It will result in Wrong Answer(WA)
- Time complexity - It will result in Time Limit Exceeded(TLE)
- Space complexity - It will result in Memory Limit Exceeded(MLE)
In algorithms contest, we need to pay attention to the time limit, memory limit, the range of input and output.
Example: A+B problem
int x;
int y;
cin >> x >> y;
cout << x+y;
1+2 is ok
1+999999999999999 will result in overflow
2) How to prove correctness?
- Prove by contradiction
- Prove by induction(Base case, inductive step)
Example: T(n) = T(n-1) + 1, T(1) = 0. Prove that T(n) = n - 1 for all n > 1 and n is an integer.
Proof:
(Base case) When n=1, T(1) = 1-1 = 0. It is correct.
(Inductive Step) Suppoer n = k, it is correct. T(k) = k - 1.
For n = k + 1, T(k+1) = T(k) + 1 = k - 1 + 1 = k. It is correct for n = k + 1.
Therefore, the algorithm is correct for all n > 0 and n is an integer.
O(1) < O(log n) < O(n) < O(nlog n) < O($n^2$) < O($n^3$) < O($2^n$)
1. Algebra
1.1 Simple Algebra Formulas:
$$\sum_{k=1}^n k^2 = \frac{n(n+1)(2n+1)}{6}$$
$$\sum_{k=1}^n k^3 = (\sum k)^2= (\frac{n(n+1)}{2})^2$$
1.2 Fast Exponentiation
How to calculate $x^k$?
$x^k = x*x*x...x$
Notice that:
$x*x = x^2$
$x^2 * x^2 = x^4$
...
double pow (double x, int k) {
if(k==0) return 1;
if(k==1) return x;
return k%2==0?pow(x,k/2)*pow(x,k/2):pow(x,k-1)*x;
}
(Important to consider special cases when you design an algorithm)
1) k is 0
2) k is 1
3) k is even and k is not 0
4) k is odd and k is not 1
2. Number Theory
2.1 Greatest Common Divisor(GCD)
gcd(x,y) - greatest integer divides both x and y.
- gcd(a,b) = gcd(a, b-a)
- gcd(a, 0) = a
- gcd(a,b) is the smallest positive number in{$ax+by | x, y \in \mathbb{Z} $ }
$x\equiv y\ (mod\ m) \Rightarrow a\%m=b\%m$
Properties:
If $a_1 \equiv b_1(mod\ m), a_2 \equiv b_2(mod m)$, then:
$a_1 +a_2 \equiv b_1+ b_2(mod\ m)$
$a_1 -a_2 \equiv b_1- b_2(mod\ m)$
$a_1 *a_2 \equiv b_1* b_2(mod\ m)$
- Euclidean algorithm
int gcd(int a, int b) {
while(b) {int r = a%b; a = b; b = r;}
return a;
}
- Extended Euclidean algorithm
Problem: Given a,b,c. Find integer solution x,y for ax+by=c.
If c % gcd(a,b) = 0, there are infinite many solutions. Otherwise, there is no solution.
long long extended_gcd(long long a, long long b, long long &x, long long &y) {
if(a==0 && b==0) return -1;
if(b==0) {x=1,y=0; return a;}
long long d=extended_gcd(b, a%b, y, x);
y -= a/b*x;
return d;
}
2.2 Prime Numbers
- For any N$\in \mathbb{Z} $,there is $N=p_1^{e1}p^{e2}_2...p^{er}_r$. And $p_1,p_2, ..., p_r$ are prime numbers. The number of factors for N is $(e1+1)(e2+1)...(er+1)$.
- Sieve's code
void getPrime(int n) {
int i, j;
bool flag[n + 1];
int prime[n + 1];
memset(flag, true, sizeof(flag)); // suppose they are all prime numbers
int count = 0; // the number of prime numbers
for(i = 2; i <= n; ++i) {
if(flag[i]) prime[++count] = i;
for(j = 1; j <= count && i*prime[j] <= n; j++) {
flag[i*prime[j]] = false;
if(i%prime[j] == 0) break;
}
}
}
2.3 Bionomial Coefficients
${n}\choose{k} $= $\frac{n(n-1)...(n-k+1)}{k!}$
Use when both n and k are small. Overflow risk.
2.4 Euler's Function
$n=p_1^{n_1} * p_2^{n_2} * ... p_k^{n_k}$
$\varphi(x) = x(1-\frac{1}{p_1})(1-\frac{1}{p_2})...(1-\frac{1}{p_k}) $
int getPhi(int x)
{
float ans = x;
for (int p=2; p*p<=n; ++p){
if (x % p == 0){
while (x % p == 0)
x /= p;
ans*=(1.0-(1.0/p));
}
}
if (x > 1)
ans*=(1.0-(1.0/x));
return (int)ans;
}
Practice Problems: (HDU, POJ, UVa - https://vjudge.net/ ; LeetCode - leetcode.com)
POJ 1061, 1142, 2262, 2407, 1811, 2447
HDU 1060, 1124, 1299, 1452, 2608, 1014, 1019, 1108, 4651
LeetCode 204
UVa 294
[Data Structures and Algorithms - 1] Introduction & Mathematics的更多相关文章
- CSIS 1119B/C Introduction to Data Structures and Algorithms
CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...
- CSC 172 (Data Structures and Algorithms)
Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- 6-1 Deque(25 分)Data Structures and Algorithms (English)
A "deque" is a data structure consisting of a list of items, on which the following operat ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- Algorithms & Data structures in C++& GO ( Lock Free Queue)
https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
随机推荐
- Knowledge Point 20180305 十进制转换成二进制浮点数
如何将十进制的浮点数 转换二进制的浮点数,分为两部分: 1. 先将整数部分转换为二进制, 2. 将小数部分转换为二进制, 然后将整数部分与小数部分相加. 以 20.5 转换为例,20转换后变为1010 ...
- 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)
Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...
- linux中删除文件内空白行的几种方法。
linux中删除文件内空白行的几种方法 有时你可能需要在 Linux 中删除某个文件中的空行.如果是的,你可以使用下面方法中的其中一个.有很多方法可以做到,但我在这里只是列举一些简单的方法. 你可能已 ...
- linux系统常用命令统计及shell特殊字符
shell 特殊字符:1.通配符2.管道 1.通配符 1.1星号(*):匹配任意长度 1.2问号(?):匹配一个长度的字符 1.3方括号([......]):匹配其中指定的字符 1.4方括号([-]) ...
- JS 创建对象总结
狭义:new 构造函数. (注:在JS中创建对象只有一种方式,就是new 构造函数.其中字面量的方式是一种语法糖,本质仍然是new 构造函数) 广义:工厂模式(解决复杂度) 构造函数模式(解决复杂度, ...
- mysql 优化(索引)
表 collect 字段 id(int 自增),title(varchar),info(text),vtype(int) 表中数据130w: select * from collect whe ...
- linux进程篇 (三) 进程间的通信3 IPC通信
3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- HDU 5212 莫比乌斯反演
Code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- gsl 复数
一.复数的表示 复数的两种表示: gsl复数结构的声明和部分宏在gsl_complex.h中,方法的声明和另一部分宏在gsl_complex_math.h.复数的表示(结构)有三种,即float型.d ...
- Windows下的SysWow64和System32
64位的Windows并不是简单地把所有东西都编译成64位就万事大吉的.关于64位的CPU应该做成什么样子,Intel和AMD曾有各自的打算.AMD的回答直接了当:新的64位处理器,应该能在提高更高处 ...