References:

1. Stanford University CS97SI by Jaehyun Park

2. Introduction to Algorithms

3. Kuangbin's ACM Template

4. Data Structures by Dayou Liu

5. Euler's Totient Function


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

  1. int x;
  2. int y;
  3. cin >> x >> y;
  4. 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.

3) Big O Noatation

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$

...

  1. double pow (double x, int k) {
  2. if(k==0) return 1;
  3. if(k==1) return x;
  4. return k%2==0?pow(x,k/2)*pow(x,k/2):pow(x,k-1)*x;
  5. }

(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
  1. int gcd(int a, int b) {
  2. while(b) {int r = a%b; a = b; b = r;}
  3. return a;
  4. }
  • 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.

  1. long long extended_gcd(long long a, long long b, long long &x, long long &y) {
  2. if(a==0 && b==0) return -1;
  3. if(b==0) {x=1,y=0; return a;}
  4. long long d=extended_gcd(b, a%b, y, x);
  5. y -= a/b*x;
  6. return d;
  7. }

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
  1. void getPrime(int n) {
  2. int i, j;
  3. bool flag[n + 1];
  4. int prime[n + 1];
  5. memset(flag, true, sizeof(flag)); // suppose they are all prime numbers
  6. int count = 0; // the number of prime numbers
  7. for(i = 2; i <= n; ++i) {
  8. if(flag[i]) prime[++count] = i;
  9. for(j = 1; j <= count && i*prime[j] <= n; j++) {
  10. flag[i*prime[j]] = false;
  11. if(i%prime[j] == 0) break;
  12. }
  13. }
  14. }

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}) $

  1. int getPhi(int x)
  2. {
  3. float ans = x;
  4. for (int p=2; p*p<=n; ++p){
  5. if (x % p == 0){
  6. while (x % p == 0)
  7. x /= p;
  8. ans*=(1.0-(1.0/p));
  9. }
  10. }
  11. if (x > 1)
  12. ans*=(1.0-(1.0/x));
  13. return (int)ans;
  14. }

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的更多相关文章

  1. CSIS 1119B/C Introduction to Data Structures and Algorithms

    CSIS 1119B/C Introduction to Data Structures and Algorithms Programming Assignment TwoDue Date: 18 A ...

  2. CSC 172 (Data Structures and Algorithms)

    Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...

  3. 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 ...

  4. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  5. 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 ...

  6. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  7. Algorithms & Data structures in C++& GO ( Lock Free Queue)

    https://github.com/xtaci/algorithms //已实现 ( Implemented ): Array shuffle https://github.com/xtaci/al ...

  8. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  9. 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 ...

随机推荐

  1. 使用 Solr 构建企业级搜索服务器

    最近因项目需要一个全文搜索引擎服务, 在考察了Lucene及Solr后,我们选择了Solr. 本文简要记录了基于Solr搭建一个企业搜索服务器的过程.网上的资料太多千篇一律,也可能版本不同,总之在参照 ...

  2. Oracle中转义下划线

    原意是查询出所有的月粒度模型,但是在oracle中,下划线也代表匹配单一任何字符,导致15分钟粒度的模型也被查询出来,在此,需要对下划线做转义,使其只表示下划线的含义,可以使用ESCAPE()函数. ...

  3. 【oracle笔记3】多表查询

    *多表查询 分类:1.合并结果集 2.连接查询 3.子查询 *合并结果集:要求被合并的表中,列的类型和列数相同. *UNION,去除重复行.完全相同的行会被去除 *UNION ALL:不去除重复行. ...

  4. 为什么我们需要DTO?

    最近在写代码时突然产生了这个疑惑,我们为什么需要DTO进行数据传输呢? 要了解DTO首先我们要知道什么是DAO,DAO就是数据库的一个数据模型,是一个类文件里面存储着数据库的字段及其getter&am ...

  5. 微信小程序新版用户授权方式处理

    最新更新(2018-12-27): 最近做了改版,做成默认进来就是首页,然后去判断有没有用户信息,没有的话再去判断用没授权过,如果授权过直接自动去获取,没有的话再跳转到授权页面.因为用户授权主要就是针 ...

  6. Java敲地鼠代码

    package test; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event. ...

  7. Product Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 产品 ...

  8. Vue 生产环境部署

    简要:继上次搭建vue环境后,开始着手vue的学习;为此向大家分享从开发环境部署到生产环境(线上)中遇到的问题和解决办法,希望能够跟各位VUE大神学习探索,如果有不对或者好的建议告知下:*~*! 一. ...

  9. SQLite学习笔记

    参考书籍 <SQLite 权威指南 第二版> Windows获取SQLite 1.主页: www.sqlite.org 2.下载 Precompiled Binaries For Wind ...

  10. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...