Skip to content

Commit

Permalink
add more content
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuanwo committed Jul 15, 2015
1 parent fe8dec4 commit f46d0b4
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 7 deletions.
13 changes: 7 additions & 6 deletions contents/0-preface1-preface.md
Expand Up @@ -3,12 +3,13 @@

## 格式要求
1. 所有代码要求使用AStyleFormat进行格式化。
2. 所有模板需给出输出参数以及输出参数,并且尽可能的标出时间复杂度。
3. 由于使用了XeLaTex进行编译,所以所有的代码块需表明使用`c++`,否则无法高亮。
4. 所有函数名,变量名力求有意义,便于理解。
5. 对其他函数有依赖的,应当在代码最开始以注释的形式标明。
6. 代码关键处应当附有简单的注释,以便于理解和使用为标准。
7. 结构体类型均使用node命名,使用到多个结构体的,则在node后加上数字序号。而在使用结构体时,应当尽可能使用有意义的名字。
1. 所有模板需给出输出参数以及输出参数,并且尽可能的标出时间复杂度。
1. 算法有中文译名的,应以中文译名为标题,英文名以加括号形式跟在中文译名后。
1. 由于使用了XeLaTex进行编译,所以所有的代码块需表明使用`c++`,否则无法高亮。
1. 所有函数名,变量名力求有意义,便于理解。
1. 对其他函数有依赖的,应当在代码最开始以注释的形式标明。
1. 代码关键处应当附有简单的注释,以便于理解和使用为标准。
1. 结构体类型均使用node命名,使用到多个结构体的,则在node后加上数字序号。而在使用结构体时,应当尽可能使用有意义的名字。

## 使用须知
1. 所有函数要求为整型的,均返回int;要求为浮点型的,均返回double。使用时请自行修改。
Expand Down
103 changes: 102 additions & 1 deletion contents/1-chapter1-number-throey.md
@@ -1,6 +1,6 @@
# 数论

## GCD
## 欧几里得算法(GCD)

输入参数:两个数a,b

Expand All @@ -14,3 +14,104 @@ int gcd(int a, int b)
return b == 0 ? a : gcd(a%b ,a);
}
```
## 扩展欧几里得算法(exGCD)
输入参数:已知a,b,求一组x,y使得p*a+q*b=Gcd(a,b)
返回参数:r(r为零时,说明存在)
时间复杂度:
```c++
int exGcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
int r = exGcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return r;
}
```

## Stein算法

输入参数:两个数a,b

返回参数:a和b的最大公约数

时间复杂度:

```c++
int Gcd(int a, int b)
{
if(a == 0) return b;
if(b == 0) return a;
if(a % 2 == 0 && b % 2 == 0) return 2 * gcd(a >> 1, b >> 1);
else if(a % 2 == 0) return gcd(a >> 1, b);
else if(b % 2 == 0) return gcd(a, b >> 1);
else return gcd(abs(a - b), Min(a, b));
}
```
## 快速筛法求素数
输入参数:无
返回参数:素数数组
时间复杂度:
```c++
const long N = 200000;
long prime[N] = {0}, num_prime = 0;
int isNotPrime[N] = {1, 1};
int main()
{
for (long i = 2 ; i < N ; i ++)
{
if (! isNotPrime[i])
prime[num_prime ++] = i;
for (long j = 0 ; j < num_prime && i * prime[j] < N ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if ( !(i % prime[j] ) )
break;
}
}
return 0;
}
```

## 快速幂

输入参数:底数A,幂次n

返回参数:rslt(A的n次方)

时间复杂度:

```c++
int qPow(int A, int n)
{
if (n == 0) return 1;
int rslt = 1;

while (n)
{
if (n & 1) //如果n为奇数
{
rslt *= A;
}
A *= A;
n >>= 1;
}
return rslt;
}
```

0 comments on commit f46d0b4

Please sign in to comment.