Skip to content

Commit a46aee9

Browse files
author
wu-kan
committed
部分博文更新
1 parent aa656f4 commit a46aee9

File tree

5 files changed

+80
-69
lines changed

5 files changed

+80
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ permalink: /about
2020

2121
基于[poole/lanyon](https://github.com/poole/lanyon)主题进行修改,感谢。
2222

23-
留言和阅读量系统基于[Valine v1.3.4](https://valine.js.org/)[LeanCloud](https://leancloud.cn/),感谢。
23+
留言和阅读量系统基于[Valine](https://valine.js.org/)[LeanCloud](https://leancloud.cn/),感谢。
2424

2525
使用了[不蒜子](http://busuanzi.ibruce.info/)页面统计,感谢。
2626

_posts/2018-11-23-单周期CPU设计.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
---
2-
title: 单周期CPU设计
32
categories: 计算机组成原理
4-
date: 2018-11-23 11:42:49
53
---
64
# 实验目的
75
1. 掌握单周期CPU数据通路图的构成、原理及其设计方法;
@@ -45,7 +43,7 @@ date: 2018-11-23 11:42:49
4543

4644
|010001|rs(5位)|rt(5位)|rd(5位)|reserved|
4745
|-|-|-|-|-|
48-
|-|-|-|-|-|
46+
||||||
4947

5048
功能:rd←rs & rt;逻辑与运算。
5149
### ori rt rs immediate
@@ -192,7 +190,7 @@ graph LR
192190
|6位|26位|
193191

194192
## 单周期CPU数据通路和控制线路图
195-
![单周期CPU数据通路和控制线路图](https://img-blog.csdnimg.cn/20181122221209604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfd2VpbGFu,size_16,color_FFFFFF,t_70)
193+
![单周期CPU数据通路和控制线路图](/public/image/2018-11-23-1.png)
196194

197195
上图是一个简单的基本上能够在单周期CPU上完成所要求设计的指令功能的数据通路和必要的控制线路图。
198196

_posts/2018-12-23-多周期CPU设计.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
---
2-
title: 多周期CPU设计
32
categories: 计算机组成原理
4-
date: 2018-12-23 11:42:49
53
---
64
# 实验目的
75
1. 认识和掌握多周期数据通路图的构成、原理及其设计方法;
@@ -196,30 +194,33 @@ graph LR
196194
|address|地址|
197195

198196
### R类型
197+
199198
|31-26|25-21|20-16|15-11|10-6|5-0|
200199
|-|-|-|-|-|-|
201200
|op|rs|rt|rd|sa|func|
202201
|6位|5位|5位|5位|5位|6位|
203202

204203
### I类型
204+
205205
|31-26|25-21|20-16|15-0|
206206
|-|-|-|-|
207207
|op|rs|rt|immediate|
208208
|6位|5位|5位|16位|
209209

210-
### J类型:
210+
### J类型
211+
211212
|31-26|25-0|
212213
|-|-|
213214
|op|address|
214215
|6位|26位|
215216

216217
## 多周期CPU状态转移图
217-
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181214161420902.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfd2VpbGFu,size_16,color_FFFFFF,t_70)
218+
![在这里插入图片描述](/public/image/2018-12-23-1.png)
218219
状态的转移有的是无条件的,例如从sIF状态转移到sID就是无条件的;有些是有条件的,例如sEXE状态之后不止一个状态,到底转向哪个状态由该指令功能,即指令操作码决定。每个状态代表一个时钟周期。
219220
## 多周期CPU控制部件的原理结构图
220-
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181214161554539.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfd2VpbGFu,size_16,color_FFFFFF,t_70)
221+
![在这里插入图片描述](/public/image/2018-12-23-2.png)
221222
## 多周期CPU数据通路和控制线路图
222-
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181214161735346.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dfd2VpbGFu,size_16,color_FFFFFF,t_70)
223+
![在这里插入图片描述](/public/image/2018-12-23-3.png)
223224
上图是一个简单的基本上能够在多周期CPU上完成所要求设计的指令功能的数据通路和必要的控制线路图。其中指令和数据各存储在不同存储器中,即有指令存储器和数据存储器。访问存储器时,先给出内存地址,然后由读或写信号控制操作。对于寄存器组,给出寄存器地址(编号),读操作时不需要时钟信号,输出端就直接输出相应数据;而在写操作时,在 WE使能信号为1时,在时钟边沿触发将数据写入寄存器。图中控制信号功能如表1所示,表2是ALU运算功能表。
224225

225226
特别提示,图上增加IR指令寄存器,目的是使指令代码保持稳定,pc写使能控制信号PCWre,是确保pc适时修改,原因都是和多周期工作的CPU有关。ADR、BDR、ALUoutDR、DBDR四个寄存器不需要写使能信号,其作用是切分数据通路,将大组合逻辑切分为若干个小组合逻辑,大延迟变为多个分段小延迟。

_posts/2019-01-31-组合数学.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ n个元素的错排数$D_n$满足递推公式:$D_1=0,D_2=1,D_n=(n−1)(D_{n−
129129
130130
通项:$D(n)=n![\frac{(-1)^2}{2!}+\ldots+\frac{(-1)^{n-1} }{(n-1)!}+\frac{(-1)^n}{n!}]=\lfloor\frac{n!}{e}+\frac{1}{2}\rfloor$
131131
## Bonuli数
132-
[使用示例](https://vjudge.net/solution/15566962)
132+
[使用示例](http://www.51nod.com/Challenge/ProblemSubmitDetail.html#!#judgeId=744601)
133133
134134
$B_n = -\frac{1}{C(n+1,n)}(C(n+1,0)B_0+C(n+1,1)B_1+\ldots+C(n+1,n-1)B_{n-1})=-\frac{1}{n+1}(C(n+1,0)B_0+C(n+1,1)B_1+\ldots+C(n+1,n-1)B_{n-1})$
135135
@@ -143,18 +143,18 @@ struct Bonuli : Factorial
143143
for (int i = b[0] = 1; i < N; ++i)
144144
{
145145
for (int j = 0; j < i; ++j)
146-
b[i] = (b[i] + mul(b[j], c(i + 1, j), M)) % M;
147-
b[i] = (M - mul(mul(fac[i], ifac[i + 1], M), b[i], M)) % M;
146+
b[i] = qadd(b[i], mul(b[j], c(i + 1, j)));
147+
b[i] = qadd(M, -mul(b[i], mul(fac[i], ifac[i + 1])));
148148
}
149149
}
150-
ll ask(ll n, int k) //return $\sum_{i=1}^ni^k\mod M$
150+
ll ask(ll n, int k)
151151
{
152-
ll r = 0, w = 1, u = (n + 1) % M;
152+
ll r = 0, w = 1, u = add(n, 1);
153153
for (int i = 1; i <= k + 1; ++i)
154-
r = (r + mul(mul(b[k + 1 - i], c(k + 1, i), M), w = mul(w, u, M), M)) % M;
155-
return mul(mul(fac[k], ifac[k + 1], M), r, M);
154+
r = qadd(r, mul(mul(b[k + 1 - i], c(k + 1, i)), w = mul(w, u)));
155+
return mul(r, mul(fac[k], ifac[k + 1]));
156156
}
157-
};
157+
} b(1 << 11, 1e9 + 7);
158158
```
159159
## Catalan数
160160
$h_1=1,h_n=\frac{4n−2}{n+1}h_{n−1}=\frac{C(2n,n)}{n+1}=C(2n,n)−C(2n,n−1)$。
@@ -173,33 +173,41 @@ $h_1=1,h_n=\frac{4n−2}{n+1}h_{n−1}=\frac{C(2n,n)}{n+1}=C(2n,n)−C(2n,n−1)
173173
```cpp
174174
struct Bell
175175
{
176-
static const int P=999999598,N=7284;
177-
int a[4],f[N],s[2][N],i,j,x;
176+
static const int P = 999999598, N = 7284;
177+
int a[4], f[N], s[2][N], i, j, x;
178178
Bell()
179-
{
180-
a[0]=2,a[1]=13,a[2]=5281,a[3]=7283;//P的质因数分解
181-
f[0]=f[1]=s[0][0]=1,s[0][1]=2;
182-
for(i=2,x=1; i<N; i++,x^=1)
183-
for(f[i]=s[x][0]=s[x^1][i-1],j=1; j<=i; ++j)
184-
s[x][j]=(s[x^1][j-1]+s[x][j-1])%P;
179+
{
180+
a[0] = 2, a[1] = 13, a[2] = 5281, a[3] = 7283; //P的质因数分解
181+
f[0] = f[1] = s[0][0] = 1, s[0][1] = 2;
182+
for (i = 2, x = 1; i < N; i++, x ^= 1)
183+
for (f[i] = s[x][0] = s[x ^ 1][i - 1], j = 1; j <= i; ++j)
184+
s[x][j] = (s[x ^ 1][j - 1] + s[x][j - 1]) % P;
185185
}
186-
int cal(int x,ll n)
187-
{
188-
int i,j,k,m=0,b[N],c[N],d[70];
189-
for(i=0; i<=x; i++)b[i]=f[i]%x;
190-
while(n)d[m++]=n%x,n/=x;
191-
for(i=1; i<m; i++)for(j=1; j<=d[i]; j++) {
192-
for(k=0; k<x; k++)c[k]=(b[k]*i+b[k+1])%x;
193-
c[x]=(c[0]+c[1])%x;
194-
for(k=0; k<=x; k++)b[k]=c[k];
186+
int cal(int x, ll n)
187+
{
188+
int i, j, k, m = 0, b[N], c[N], d[70];
189+
for (i = 0; i <= x; i++)
190+
b[i] = f[i] % x;
191+
while (n)
192+
d[m++] = n % x, n /= x;
193+
for (i = 1; i < m; i++)
194+
for (j = 1; j <= d[i]; j++)
195+
{
196+
for (k = 0; k < x; k++)
197+
c[k] = (b[k] * i + b[k + 1]) % x;
198+
c[x] = (c[0] + c[1]) % x;
199+
for (k = 0; k <= x; k++)
200+
b[k] = c[k];
195201
}
196202
return c[d[0]];
197203
}
198204
ll ask(ll n)
199-
{
200-
if(n<N)return f[n];
201-
ll t=0;
202-
for(int i=0; i<4; ++i)t=(t+(P/a[i])*pow(P/a[i],a[i]-2,a[i])%P*cal(a[i],n)%P)%P;
205+
{
206+
if (n < N)
207+
return f[n];
208+
ll t = 0;
209+
for (int i = 0; i < 4; ++i)
210+
t = (t + (P / a[i]) * pow(P / a[i], a[i] - 2, a[i]) % P * cal(a[i], n) % P) % P;
203211
return t;
204212
}
205213
};

_posts/2019-02-01-数论.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ ll gcd(ll a, ll b) //无取模求gcd
4444
#### 推论
4545
$a,b$互质的充要条件是$ax+by=1$有整数解。
4646
## 同余系运算
47+
三种乘法的比较,结论是如果可以用`__int128`就用,否则就用第一种:
48+
- [使用`__int128`](https://vjudge.net/solution/19521824)
49+
- [第一种同余乘法](https://vjudge.net/solution/19521784)
50+
- [第二种同余乘法](https://vjudge.net/solution/19521800)
51+
4752
求乘法逆元的另外一种方法是用欧拉定理$x^{\phi(m)}\equiv1\pmod m$,x的逆是$x^{\phi(m)-1}$。特别地,m为素数时$\phi(m)=m-1$,此时x的逆就是`pow(x,m-2,m)`。
4853
4954
log函数:m为素数时[求解模方程](https://vjudge.net/solution/19395246)$a^x\equiv b\pmod m$。设P为质数,G为P的原根,则$x^y\equiv b\pmod P$等价于$y\ ind\ x\equiv b\pmod{P-1}$,其中$G\ ind\ x\equiv x\pmod P$。
@@ -52,17 +57,16 @@ struct Mod
5257
{
5358
const ll M, SM;
5459
Mod(ll M) : M(M), SM(sqrt(M) + 0.5) {}
55-
ll qadd(ll a, ll b) const { return a += b, a < M ? a : a - M; } //假如a和b都已经在同余系内,就不必取模了,取模运算耗时很高
56-
ll add(ll a, ll b) const { return a = (a + b) % M, a < 0 ? a + M : a; } //考虑a和b不在同余系内甚至为负数的情况
60+
ll qadd(ll a, ll b) const { return a += b, a < M ? a : a - M; } //假如a和b都已经在同余系内,就不必取模了,取模运算耗时很高
61+
ll add(ll a, ll b) const { return qadd((a + b) % M, M); } //考虑a和b不在同余系内甚至为负数的情况
5762
ll mul(ll a, ll b) const { return add(a * b, 0); }
5863
/*
59-
ll mul(ll x, ll y) const //无循环快速计算同余乘法,根据a*b是否爆ll替换a*b%m
64+
ll mul(ll a, ll b) const //无循环快速计算同余乘法,根据a*b是否爆ll替换a*b%m,需要0<=a<M且0<=b<M,可以调用时手动取模
6065
{
61-
ll a = x / SM, b = x % SM,
62-
c = y / SM, d = y % SM,
63-
e = SM * SM - M, //可能为负
64-
f = ((a * d + b * c) % M + a * c / SM * e) % M;
65-
return add(((a * c % SM + f / SM) * e % M + f % SM * SM) % M, b * d);
66+
ll c = a / SM, d = b / SM, e = SM * SM - M;
67+
a %= SM, b %= SM;
68+
ll f = (qadd(a * d, b * c) + c * d / SM * e) % M;
69+
return add(((c * d % SM + f / SM) * e % M) % M + f % SM * SM, a * b);
6670
}
6771
*/
6872
ll pow(ll a, ll b) const
@@ -73,26 +77,13 @@ struct Mod
7377
r = mul(r, a);
7478
return r;
7579
}
76-
ll inv(ll a) const { return pow(a, M - 2); } //return pow(a, phi(M) - 1);
80+
ll inv(ll a) const { return pow(a, M - 2); }
7781
/*
78-
ll inv(ll a) const //模m下a的乘法逆元,不存在返回-1(m为素数时a不为0必有逆元)
82+
ll inv(ll a) const //M不为素数时a的乘法逆元,不存在返回-1(m为素数时a不为0必有逆元)
7983
{
8084
ll x, y, d = gcd(a, m, x, y);
81-
return d == 1 ? (x + m) % m : -1;
82-
}
83-
*/
84-
ll log(ll a, ll b) const
85-
{
86-
unordered_map<ll, ll> x;
87-
for (ll i = 0, e = 1; i <= SM; ++i, e = mul(e, a))
88-
if (!x.count(e))
89-
x[e] = i;
90-
for (ll i = 0, v = inv(pow(a, SM)); i <= SM; ++i, b = mul(b, v))
91-
if (x.count(b))
92-
return i * SM + x[b];
93-
return -1;
85+
return d == 1 ? (x + m) % m : -1; //return pow(a, phi(M) - 1);
9486
}
95-
/*
9687
vector<ll> sol(ll a, ll b) const //解同余方程,返回ax=b(mod M)循环节内所有解
9788
{
9889
vector<ll> ans;
@@ -105,6 +96,17 @@ struct Mod
10596
return ans;
10697
}
10798
*/
99+
ll log(ll a, ll b) const
100+
{
101+
unordered_map<ll, ll> x;
102+
for (ll i = 0, e = 1; i <= SM; ++i, e = mul(e, a))
103+
if (!x.count(e))
104+
x[e] = i;
105+
for (ll i = 0, v = inv(pow(a, SM)); i <= SM; ++i, b = mul(b, v))
106+
if (x.count(b))
107+
return i * SM + x[b];
108+
return -1;
109+
}
108110
};
109111
```
110112
### 中国剩余定理解同余方程组
@@ -207,15 +209,16 @@ struct PollardRho
207209
{
208210
bool isPrime(ll n, int S = 12) //MillerRabin素数测试,S为测试次数,用前S个素数测一遍,S=12可保证unsigned long long范围内无错;n<2请特判
209211
{
210-
ll d, u, t, p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
212+
static ll d, u, t, p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
211213
for (d = n - 1; !(d & 1);)
212214
d >>= 1; //未对0,1做特判!
215+
Mod mo(n);
213216
for (int i = 0; i < S; ++i)
214217
{
215-
if (n % p[i] == 0)
218+
if (!(n % p[i]))
216219
return n == p[i];
217-
for (u = d, t = pow(p[i], u, n); u != n - 1 && t != n - 1 && t != 1; u <<= 1)
218-
t = mul(t, t, n);
220+
for (t = mo.pow(p[i], u = d); t != n - 1 && t != 1 && u != n - 1;)
221+
t = mo.mul(t, t), u <<= 1;
219222
if (t != n - 1 && !(u & 1))
220223
return 0;
221224
}
@@ -225,12 +228,13 @@ struct PollardRho
225228
{
226229
if (isPrime(n))
227230
return factor.push_back(n);
231+
Mod mo(n);
228232
for (ll c = 1;; ++c)
229233
for (ll i = 0, k = 1, x = rand() % (n - 1) + 1, y, p;;)
230234
{
231235
if (++i == k)
232236
y = x, k <<= 1;
233-
if (x = (mul(x, x, n) + c) % n, p = gcd(abs(x - y), n), p == n)
237+
if (x = mo.add(mo.mul(x, x), c), p = gcd(abs(x - y), n), p == n)
234238
break;
235239
if (p > 1)
236240
return fac(p, factor), fac(n / p, factor);

0 commit comments

Comments
 (0)