提交 d6c5f45a 编写于 作者: 每日一练社区's avatar 每日一练社区

add 8 exercises

上级 25e421c2
......@@ -32,7 +32,7 @@
2
```
以下选项错误的是?
## aop
......
......@@ -18,7 +18,7 @@
请你进行10万次模拟,计算出甲队夺冠的概率。
以下选项错误的是?
## aop
......
......@@ -46,6 +46,8 @@ J大臣想知道:他从某一个城市出发,中间不休息,到达另一
大臣J从城市4到城市5要花费135的路费。
```
下面错误的一项是?
## aop
### before
......@@ -53,76 +55,58 @@ J大臣想知道:他从某一个城市出发,中间不休息,到达另一
```cpp
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long
const double eps = 3e-8;
const int mod = 10;
const int maxn = 10005;
vector<int> ed[maxn];
ll edge[maxn][maxn];
ll dis[maxn];
ll sum = 0;
```
### after
```cpp
int main()
{
int n, a, b;
ll c;
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
scanf("%d%d%lld", &a, &b, &c);
ed[a].push_back(b);
ed[b].push_back(a);
edge[a][b] = c;
edge[b][a] = c;
}
int starta = 1;
int endnode, startnode;
sum = 0;
endnode = bfs(starta);
sum = 0;
startnode = bfs(endnode);
double ans = sum * (sum + 1.0) / 2 + 10.0 * sum;
printf("%.0f\n", ans);
}
```
## 答案
```cpp
int bfs(int node)
struct edge
{
mem(dis, -1);
queue<int> que;
que.push(node);
int ans = node;
dis[node] = 0;
while (!que.empty())
{
int now = que.front();
que.pop();
for (int i = 0; i < ed[now].size(); i++)
{
int temp = ed[now][i];
if (dis[temp] < 0)
{
dis[temp] = dis[now] + edge[now][temp];
if (dis[temp] > sum)
int to, cost;
};
vector<vector<edge>> G;
vector<bool> vst;
int n;
int ans, dis;
void dfs(int u, int d)
{
vst[u] = true;
if (d > dis)
ans = u, dis = d;
for (int i = 0; i < G[u].size(); i++)
{
ans = temp;
sum = dis[temp];
}
que.push(temp);
}
}
int son = G[u][i].to;
if (!vst[son])
dfs(son, G[u][i].cost);
}
vst[u] = false;
}
int find(int u)
{
ans = dis = 0;
dfs(u, 0);
return ans;
}
int main()
{
scanf("%d", &n);
G.resize(n + 5), vst.resize(n + 5);
for (int i = 1; i < n; i++)
{
int p, q, d;
scanf("%d %d %d", &p, &q, &d);
G[p].push_back(edge{q, d});
G[q].push_back(edge{p, d});
}
int x = find(1), y = find(x);
printf("%d", dis * (dis + 1) / 2 + 10 * dis);
return 0;
}
```
## 选项
......@@ -130,66 +114,139 @@ int bfs(int node)
### A
```cpp
int bfs(int node)
const int maxd = 10000 + 50;
struct Point
{
mem(dis, -1);
queue<int> que;
que.push(node);
int ans = node;
dis[node] = 0;
while (!que.empty())
{
int now = que.front();
que.pop();
for (int i = 0; i < ed[now].size(); i++)
{
int temp = ed[now][i];
if (dis[temp] < 0)
map<int, int> m;
int status;
} p[maxd];
int r = 0, gps;
void f(int x, int ctn)
{
p[x].status = 1;
for (map<int, int>::iterator it = p[x].m.begin(); it != p[x].m.end(); it++)
{
dis[temp] = dis[now] + edge[now][temp];
ans = temp;
sum = dis[temp];
que.push(temp);
if (p[it->first].status == 1)
continue;
f(it->first, ctn + it->second);
}
p[x].status = 0;
if (r < ctn)
{
r = ctn;
gps = x;
}
}
int main()
{
int n, i, x, y, l;
cin >> n;
for (i = 0; i < n - 1; i++)
{
cin >> x >> y >> l;
p[x].m[y] = l;
p[y].m[x] = l;
}
return ans;
f(1, 0);
f(gps, 0);
cout << (21 + r) * r / 2;
return 0;
}
```
### B
```cpp
int bfs(int node)
#define _for(i, a, b) for (int i = a; i < b; i++)
#define _unfor(i, a, b) for (int i = a; i >= b; i--)
#define RI(a) scanf("%d", &a)
using namespace std;
typedef long long LL;
int d[111111], n, vis[111111], maxh = 0, p_h[111111], ans = 0;
vector<int> son[111111], G[111111], h_p[111111];
map<pair<int, int>, int> W;
int main()
{
mem(dis, -1);
queue<int> que;
que.push(node);
int ans = node;
dis[node] = 0;
while (!que.empty())
{
int now = que.front();
que.pop();
for (int i = 0; i < ed[now].size(); i++)
RI(n);
_for(i, 1, n)
{
int temp = ed[now][i];
dis[temp] = dis[now] + edge[now][temp];
int a, b, c;
RI(a);
RI(b);
RI(c);
G[a].push_back(b);
G[b].push_back(a);
pair<int, int> pr(a > b ? a : b, a > b ? b : a);
W[pr] = c;
}
ans = temp;
sum = dis[temp];
queue<int> q;
q.push(1);
h_p[0].push_back(1);
while (!q.empty())
{
int v = q.front(), len = G[v].size();
q.pop();
vis[v] = 1;
_for(k, 0, len)
{
int e = G[v][k];
if (!vis[e])
{
vis[e] = 1;
son[v].push_back(e);
q.push(e);
maxh = max(maxh, p_h[e] = p_h[v] + 1);
h_p[p_h[e]].push_back(e);
}
}
}
que.push(temp);
_unfor(h, maxh, 0)
{
int len = h_p[h].size();
_for(k, 0, len)
{
int i = h_p[h][k], _max = 0, _cmax = 0;
int len2 = son[i].size();
_for(k2, 0, len2)
{
int j = son[i][k2], t;
pair<int, int> pr(i > j ? i : j, i > j ? j : i);
t = W[pr] + d[j];
if (t > _cmax)
if ((_cmax = t) > _max)
swap(_max, _cmax);
ans = max(ans, _max + _cmax);
}
d[i] = _max;
}
return ans;
}
cout << (LL)(ans + 1) * ans / 2 + ans * 10 << endl;
}
```
### C
```cpp
#define mem(a, b) memset(a, b, sizeof(a))
#define ll long long
const double eps = 3e-8;
const int mod = 10;
const int maxn = 10005;
vector<int> ed[maxn];
ll edge[maxn][maxn];
ll dis[maxn];
ll sum = 0;
int bfs(int node)
{
mem(dis, -1);
......@@ -207,7 +264,7 @@ int bfs(int node)
if (dis[temp] < 0)
{
dis[temp] = dis[now] + edge[now][temp];
if (dis[temp] < sum)
if (dis[temp] > sum)
{
ans = temp;
sum = dis[temp];
......@@ -218,4 +275,28 @@ int bfs(int node)
}
return ans;
}
int main()
{
int n, a, b;
ll c;
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
scanf("%d%d%lld", &a, &b, &c);
ed[a].push_back(b);
ed[b].push_back(a);
edge[a][b] = c;
edge[b][a] = c;
}
int starta = 1;
int endnode, startnode;
sum = 0;
endnode = bfs(starta);
sum = 0;
startnode = bfs(endnode);
double ans = sum * (sum + 1.0) / 2 + 10.0 * sum;
printf("%.0f\n", ans);
}
```
......@@ -18,53 +18,18 @@
### before
```cpp
#include <iostream>
using namespace std;
int ans;
int dire[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int vis[7][7];
```
### after
```cpp
int main()
{
dfs(3, 3);
cout << ans / 4 << endl;
return 0;
}
```
## 答案
```cpp
void dfs(int x, int y)
{
if (x == 0 || y == 0 || x == 6 || y == 6)
{
ans++;
return;
}
vis[x][y] = 1;
vis[6 - x][6 - y] = 1;
for (int k = 0; k < 4; k++)
{
int nx = x + dire[k][0];
int ny = y + dire[k][1];
if (nx < 0 || nx > 6 || ny < 0 || ny > 6)
continue;
if (!vis[nx][ny])
{
dfs(nx, ny);
}
}
vis[x][y] = 0;
vis[6 - x][6 - y] = 0;
}
509
```
## 选项
......@@ -72,92 +37,17 @@ void dfs(int x, int y)
### A
```cpp
void dfs(int x, int y)
{
if (x == 0 || y == 0 || x == 6 || y == 6)
{
ans++;
return;
}
vis[x][y] = 1;
vis[6 - x][6 - y] = 1;
for (int k = 0; k < 4; k++)
{
int nx = x + dire[k][0];
int ny = y + dire[k][1];
if (nx < 0 || nx > 6 || ny < 0 || ny > 6)
continue;
if (!vis[nx][ny])
{
dfs(nx - 1, ny - 1);
}
}
vis[x][y] = 0;
vis[6 - x][6 - y] = 0;
}
508
```
### B
```cpp
void dfs(int x, int y)
{
if (x == 0 || y == 0 || x == 6 || y == 6)
{
ans++;
return;
}
vis[x][y] = 1;
vis[6 - x][6 - y] = 1;
for (int k = 0; k < 4; k++)
{
int nx = x + dire[k][0];
int ny = y + dire[k][1];
if (nx < 0 || nx > 6 || ny < 0 || ny > 6)
continue;
if (!vis[nx][ny])
{
dfs(nx + 1, ny + 1);
}
}
vis[x][y] = 0;
vis[6 - x][6 - y] = 0;
}
510
```
### C
```cpp
void dfs(int x, int y)
{
if (x == 0 || y == 0 || x == 6 || y == 6)
{
ans++;
return;
}
vis[x][y] = 1;
vis[6 - x][6 - y] = 1;
for (int k = 0; k < 4; k++)
{
int nx = x + dire[k][0];
int ny = y + dire[k][1];
if (nx < 0 || nx > 6 || ny < 0 || ny > 6)
continue;
if (!vis[nx][ny])
{
dfs(nx, ny + 1);
}
}
vis[x][y] = 0;
vis[6 - x][6 - y] = 0;
}
600
```
......@@ -19,59 +19,19 @@ A + —- + ——–- = 10
### before
```cpp
#include <stdio.h>
#include <stdbool.h>
int ans = 0;
int nums[10];
bool visited[10];
void judge()
{
int i;
if (nums[0] + (double)nums[1] / nums[2] + (double)(nums[3] * 100 + nums[4] * 10 + nums[5]) / (nums[6] * 100 + nums[7] * 10 + nums[8]) == 10)
{
printf("%d + %d/%d + %d%d%d/%d%d%d", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5], nums[6], nums[7], nums[8]);
printf("\n");
ans++;
}
}
```
### after
```cpp
int main()
{
dfs(0);
printf("%d\n", ans);
return 0;
}
```
## 答案
```cpp
void dfs(int index)
{
if (index >= 9)
{
judge();
return;
}
int i;
for (i = 1; i < 10; i++)
{
if (visited[i] == false)
{
visited[i] = true;
nums[index] = i;
dfs(index + 1);
visited[i] = false;
}
}
}
29
```
## 选项
......@@ -79,71 +39,17 @@ void dfs(int index)
### A
```cpp
void dfs(int index)
{
if (index >= 9)
{
judge();
return;
}
int i;
for (i = 1; i < 10; i++)
{
if (visited[i] == false)
{
visited[i] = true;
nums[index] = i;
dfs(index);
visited[i] = false;
}
}
}
28
```
### B
```cpp
void dfs(int index)
{
if (index >= 9)
{
judge();
return;
}
int i;
for (i = 1; i < 10; i++)
{
if (visited[i] == true)
{
visited[i] = false;
nums[index] = i;
dfs(index);
visited[i] = true;
}
}
}
30
```
### C
```cpp
void dfs(int index)
{
if (index >= 9)
{
judge();
return;
}
int i;
for (i = 1; i < 10; i++)
{
if (visited[i] == true)
{
visited[i] = false;
nums[index] = i;
dfs(index + 1);
visited[i] = true;
}
}
}
31
```
......@@ -11,47 +11,57 @@ masks = [9090400, 8499400, 5926800, 8547000, 4958200, 4422600, 5751200, 4175600,
请你计算这个差最小是多少?
下面的代码实现了这一功能,请你填补空白处的内容。
```cpp
#include <bits/stdc++.h>
using namespace std;
int ans = 0x3f3f3f3f;
int num[15] =
{
9090400, 8499400, 5926800,
8547000, 4958200, 4422600, 5751200, 4175600, 6309600,
5865200, 6604400, 4635000, 10663400, 8087200, 4554000};
void dfs(int u, int s1, int s2)
{
if (u == 15)
{
ans = min(ans, abs(s1 - s2));
return;
}
dfs(u + 1, s1 + num[u], s2);
__________________
}
int main()
{
dfs(0, 0, 0);
cout << ans << endl;
return 0;
}
```
## aop
### before
```cpp
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
long int masks[15] = {9090400, 8499400, 5926800, 8547000, 4958200,
4422600, 5751200, 4175600, 6309600,
5865200, 6604400, 4635000, 10663400, 8087200, 4554000};
long ans = 1000000000;
```
### after
```cpp
int main()
{
dfs(0, 0, 0);
cout << ans;
}
```
## 答案
```cpp
void dfs(int n, long h1, long h2)
{
if (n == 15)
{
ans = min(ans, abs(h1 - h2));
return;
}
dfs(n + 1, h1 + masks[n], h2);
dfs(n + 1, h1, h2 + masks[n]);
}
dfs(u + 1, s1, s2 + num[u]);
```
## 选项
......@@ -59,44 +69,17 @@ void dfs(int n, long h1, long h2)
### A
```cpp
void dfs(int n, long h1, long h2)
{
if (n == 15)
{
ans = min(ans, abs(h1 - h2));
return;
}
dfs(n + 1, h1 + masks[n + 1], h2);
dfs(n + 1, h1, h2 + masks[n + 1]);
}
dfs(u, s1, s2 + num[u]);
```
### B
```cpp
void dfs(int n, long h1, long h2)
{
if (n == 15)
{
ans = min(ans, abs(h1 - h2));
return;
}
dfs(n + 1, h1 + masks[n + 1], h2);
dfs(n + 1, h1, h2 + masks[n]);
}
dfs(u + 1, s1 + num[u], s2 + num[u]);
```
### C
```cpp
void dfs(int n, long h1, long h2)
{
if (n == 15)
{
ans = min(ans, abs(h1 - h2));
return;
}
dfs(n, h1 + masks[n], h2);
dfs(n, h1, h2 + masks[n]);
}
dfs(u + 1, s1 + num[u], s2);
```
......@@ -28,79 +28,18 @@
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
int row = 4, col;
int seq[5][8];
int tmp[11];
int cnt = 0;
bool used[10];
void change()
{
int cur = 1;
for (int i = 1; i <= row; ++i)
{
for (int j = 1; j <= i; ++j)
{
seq[i][j] = tmp[cur++];
}
}
}
bool judge()
{
for (int i = 1; i <= row - 1; ++i)
{
for (int j = 1; j <= i; ++j)
{
if (seq[i][j] > seq[i + 1][j] || seq[i][j] > seq[i + 1][j + 1])
return false;
}
}
return true;
}
int ans = 0;
```
### after
```cpp
int main()
{
memset(used, false, sizeof(used));
dfs(1);
cout << cnt << endl;
return 0;
}
```
## 答案
```cpp
void dfs(int idx)
{
if (idx == 11)
{
change();
if (judge())
{
cnt++;
return;
}
return;
}
for (int i = 0; i <= 9; ++i)
{
if (!used[i])
{
tmp[idx] = i;
used[i] = true;
dfs(idx + 1);
used[i] = false;
}
}
}
768
```
## 选项
......@@ -108,86 +47,17 @@ void dfs(int idx)
### A
```cpp
void dfs(int idx)
{
if (idx == 11)
{
change();
if (judge())
{
cnt++;
return;
}
return;
}
for (int i = 0; i <= 9; ++i)
{
if (!used[i])
{
tmp[idx] = i;
used[i] = true;
dfs(idx);
used[i] = false;
}
}
}
767
```
### B
```cpp
void dfs(int idx)
{
if (idx == 11)
{
change();
if (judge())
{
cnt++;
return;
}
return;
}
for (int i = 0; i <= 9; ++i)
{
if (!used[i])
{
tmp[idx] = i;
used[i] = false;
dfs(idx);
used[i] = true;
}
}
}
769
```
### C
```cpp
void dfs(int idx)
{
if (idx == 11)
{
change();
if (judge())
{
cnt++;
return;
}
return;
}
for (int i = 0; i <= 9; ++i)
{
if (!used[i])
{
tmp[idx] = i;
used[i] = false;
dfs(idx + 1);
used[i] = true;
}
}
}
770
```
......@@ -18,59 +18,64 @@
47
```
## aop
### before
下面的代码实现了这一功能,请你补全空白处。
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
const int maxn = 100005;
int numv[maxn];
int v[maxn];
```
### after
int dfs(int idx)
{
if (idx == 0)
return 0;
```cpp
if (idx == 1)
return v[0] * v[1];
int main(){
int max_, a = 0x3f3f3f, b = 0x3f3f3f3f, c = 0;
if (idx >= 2)
__________________
b = v[idx] * numv[idx - 1] + dfs(idx - 1);
c = min(a, b);
return c;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&v[i]);
if(i==0) numv[i]=v[i];
numv[i]=numv[i-1]+v[i];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &v[i]);
if (i == 0)
numv[i] = v[i];
numv[i] = numv[i - 1] + v[i];
}
printf("%d",dfs(n-1));
printf("%d", dfs(n - 1));
}
```
## 答案
## aop
### before
```cpp
int dfs(int idx){
if(idx==0)
return 0;
```
### after
if(idx==1)
return v[0]*v[1];
```cpp
int max_,a=0x3f3f3f,b=0x3f3f3f3f,c=0;
if(idx>=2)
a=(v[idx]*v[idx-1]+dfs(idx-2)+numv[idx-2]*(v[idx]+v[idx-1]));
b=v[idx]*numv[idx-1]+dfs(idx-1);
c=min(a,b);
return c;
}
```
## 答案
```cpp
a = (v[idx] * v[idx - 1] + dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1]));
```
## 选项
......@@ -78,59 +83,17 @@ int dfs(int idx){
### A
```cpp
int dfs(int idx)
{
if (idx == 0)
return 0;
if (idx == 1)
return v[0] * v[1];
int max_, a = 0x3f3f3f, b = 0x3f3f3f3f, c = 0;
if (idx >= 2)
a = (v[idx] * v[idx] + dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1]));
b = v[idx] * numv[idx] + dfs(idx - 1);
c = min(a, b);
return c;
}
a = (v[idx] * v[idx - 1] + dfs(idx - 2));
```
### B
```cpp
int dfs(int idx)
{
if (idx == 0)
return 0;
if (idx == 1)
return v[0] * v[1];
int max_, a = 0x3f3f3f, b = 0x3f3f3f3f, c = 0;
if (idx >= 2)
a = (v[idx - 2] * v[idx] + dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1]));
b = v[idx - 2] * numv[idx] + dfs(idx - 1);
c = min(a, b);
return c;
}
a = (dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1]));
```
### C
```cpp
int dfs(int idx)
{
if (idx == 0)
return 0;
if (idx == 1)
return v[0] * v[1];
int max_, a = 0x3f3f3f, b = 0x3f3f3f3f, c = 0;
if (idx >= 2)
a = (v[idx] * v[idx] + dfs(idx - 2) + numv[idx - 2] * (v[idx] + v[idx - 1]));
b = v[idx - 1] * numv[idx] + dfs(idx - 1);
c = min(a, b);
return c;
}
a = (v[idx] * v[idx - 1] + dfs(idx - 1) + numv[idx + 2] * (v[idx] + v[idx - 1]));
```
......@@ -13,29 +13,13 @@ x星球有很多高耸入云的高塔,刚好可以用来做耐摔测试。塔
某次测试的塔高为1000层,如果我们总是采用最佳策略,在最坏的运气下最多需要测试多少次才能确定手机的耐摔指数呢?
## aop
### before
下面的程序实现了这一功能,请你补全空白处的代码:
```cpp
#include <iostream>
using namespace std;
int num[5][1010] = {0};
```
### after
```cpp
int main()
{
cout << dp(3, 1000) << endl;
return 0;
}
```
## 答案
```cpp
int dp(int k, int n)
{
int res = 10000;
......@@ -47,11 +31,36 @@ int dp(int k, int n)
return num[k][n];
for (int i = 1; i <= n; i++)
{
res = min(res, max(dp(k - 1, i - 1), dp(k, n - i)) + 1);
__________________
}
num[k][n] = res;
return res;
}
int main()
{
cout << dp(3, 1000) << endl;
return 0;
}
```
## aop
### before
```cpp
```
### after
```cpp
```
## 答案
```cpp
res = min(res, max(dp(k - 1, i - 1), dp(k, n - i)) + 1);
```
## 选项
......@@ -59,62 +68,17 @@ int dp(int k, int n)
### A
```cpp
int dp(int k, int n)
{
int res = 10000;
if (n == 0)
return 0;
if (k == 1)
return n;
if (num[k][n])
return num[k][n];
for (int i = 1; i <= n; i++)
{
res = max(res, min(dp(k - 1, i - 1), dp(k, n - 1)) + 1);
}
num[k][n] = res;
return res;
}
res = min(res, max(dp(k, i - 1), dp(k, n - i)) + 1);
```
### B
```cpp
int dp(int k, int n)
{
int res = 10000;
if (n == 0)
return 0;
if (k == 1)
return n;
if (num[k][n])
return num[k][n];
for (int i = 1; i <= n; i++)
{
res = min(res, max(dp(k + 1, i + 1), dp(k, n - i)) + 1);
}
num[k][n] = res;
return res;
}
res = min(res, max(dp(k - 1, i), dp(k, n - i)) + 1);
```
### C
```cpp
int dp(int k, int n)
{
int res = 10000;
if (n == 0)
return 0;
if (k == 1)
return n;
if (num[k][n])
return num[k][n];
for (int i = 1; i <= n; i++)
{
res = min(res, max(dp(k - 1, i), dp(k, n - i)) + 1);
}
num[k][n] = res;
return res;
}
res = min(res, max(dp(k - 1, i - n), dp(k, n - i)) + 1);
```
......@@ -49,18 +49,15 @@ X 国王有一个地宫宝库,是 n×m 个格子的矩阵,每个格子放一
14
```
以下选项错误的是?
## aop
### before
```cpp
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
const int MOD = 1e9 + 7;
int dp[N][N][13][14];
int g[N][N];
int n, m, k;
```
### after
......@@ -71,48 +68,56 @@ int n, m, k;
## 答案
```cpp
int main()
const int N = 55, M = 15, mod = 1e9 + 7;
int w[N][N];
int memo[N][N][M][M];
int n, m, K, ans;
int dfs(int i, int j, int k, int c)
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
if (memo[i][j][k][c] != -1)
return memo[i][j][k][c];
if (i == n && j == m)
{
for (int j = 1; j <= m; j++)
if (k == K || (k == K - 1 && w[i][j] > c))
return 1;
else
return 0;
}
int sum = 0;
if (i + 1 <= n)
{
cin >> g[i][j];
g[i][j]++;
if (w[i][j] > c)
sum = (sum + dfs(i + 1, j, k + 1, w[i][j])) % mod;
sum = (sum + dfs(i + 1, j, k, c)) % mod;
}
if (j + 1 <= m)
{
if (w[i][j] > c)
sum = (sum + dfs(i, j + 1, k + 1, w[i][j])) % mod;
sum = (sum + dfs(i, j, k, c)) % mod;
}
dp[1][1][1][g[1][1]] = 1;
dp[1][1][0][0] = 1;
return memo[i][j][k][c] = sum % mod;
}
int main()
{
scanf("%d %d %d", &n, &m, &K);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
for (int u = 0; u <= k; u++)
{
for (int v = 0; v <= 13; v++)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
if (u > 0 && v == g[i][j])
{
for (int c = 0; c < v; c++)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u - 1][c]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
}
}
}
}
scanf("%d", &w[i][j]);
w[i][j]++;
}
}
int res = 0;
for (int i = 0; i <= 13; i++)
res = (res + dp[n][m][k][i]) % MOD;
cout << res << endl;
memset(memo, -1, sizeof(memo));
dfs(1, 1, 0, 0);
printf("%d\n", memo[1][1][0][0]);
return 0;
}
```
## 选项
......@@ -120,45 +125,93 @@ int main()
### A
```cpp
int main()
int n, m, k;
int data[55][55];
typedef long long LL;
LL ans;
LL mod = 1000000007;
LL cache[55][55][15][15];
LL dfs2(int x, int y, int max, int count)
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
if (cache[x][y][max + 1][count] != -1)
{
cin >> g[i][j];
g[i][j]++;
return cache[x][y][max + 1][count];
}
LL ans = 0;
if (x == n || y == m || count > k)
{
return 0;
}
dp[1][1][1][g[1][1]] = 1;
dp[1][1][0][0] = 1;
for (int i = 1; i <= n; i++)
int cur = data[x][y];
if (x == n - 1 && y == m - 1)
{
for (int j = 1; j <= m; j++)
if (count == k || (count == k - 1 && cur > max))
{
for (int u = 0; u <= k; u++)
ans++;
if (ans > mod)
{
for (int v = 0; v <= 13; v++)
ans %= mod;
}
}
return ans;
}
if (cur > max)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
if (u > 0 && v == g[i][j])
ans += dfs2(x, y + 1, cur, count + 1);
ans += dfs2(x + 1, y, cur, count + 1);
}
ans += dfs2(x, y + 1, max, count);
ans += dfs2(x + 1, y, max, count);
cache[x][y][max + 1][count] = ans % mod;
return ans % mod;
}
void dfs(int x, int y, int max, int count)
{
if (x == n || y == m || count > k)
{
for (int c = 0; c < v; c++)
return;
}
int cur = data[x][y];
if (x == n - 1 && y == m - 1)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j][u - 1][c]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
if (count == k || (count == k - 1 && cur > max))
{
ans++;
if (ans > mod)
{
ans %= mod;
}
}
}
if (cur > max)
{
dfs(x, y + 1, cur, count + 1);
dfs(x + 1, y, cur, count + 1);
}
dfs(x, y + 1, max, count);
dfs(x + 1, y, max, count);
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
scanf("%d", &data[i][j]);
}
}
int res = 0;
for (int i = 0; i <= 13; i++)
res = (res + dp[n][m][k][i]) % MOD;
cout << res << endl;
memset(cache, -1, sizeof(cache));
printf("%lld\n", dfs2(0, 0, -1, 0));
return 0;
}
```
......@@ -166,45 +219,53 @@ int main()
### B
```cpp
int main()
int n, m, k;
int data[55][55];
long long ans;
long long mod = 1000000007;
void dfs(int x, int y, int max, int count)
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
if (x == n || y == m)
{
cin >> g[i][j];
g[i][j]++;
return;
}
}
dp[1][1][1][g[1][1]] = 1;
dp[1][1][0][0] = 1;
for (int i = 1; i <= n; i++)
int cur = data[x][y];
if (x == n - 1 && y == m - 1)
{
for (int j = 1; j <= m; j++)
if (count == k || (count == k - 1 && cur > max))
{
for (int u = 0; u <= k; u++)
ans++;
if (ans > mod)
{
for (int v = 0; v <= 13; v++)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][v]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u][v]) % MOD;
if (u > 0 && v == g[i][j])
{
for (int c = 0; c < v; c++)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u - 1][c]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j][u - 1][c]) % MOD;
ans %= mod;
}
}
}
if (cur > max)
{
dfs(x, y + 1, cur, count + 1);
dfs(x + 1, y, cur, count + 1);
}
dfs(x, y + 1, max, count);
dfs(x + 1, y, max, count);
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
scanf("%d", &data[i][j]);
}
}
int res = 0;
for (int i = 0; i <= 13; i++)
res = (res + dp[n][m][k][i]) % MOD;
cout << res << endl;
dfs(0, 0, -1, 0);
printf("%lld\n", ans);
return 0;
}
```
......@@ -212,6 +273,12 @@ int main()
### C
```cpp
const int N = 55;
const int MOD = 1e9 + 7;
int dp[N][N][13][14];
int g[N][N];
int n, m, k;
int main()
{
cin >> n >> m >> k;
......@@ -239,7 +306,7 @@ int main()
{
for (int c = 0; c < v; c++)
{
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u][c]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i - 1][j][u - 1][c]) % MOD;
dp[i][j][u][v] = (dp[i][j][u][v] + dp[i][j - 1][u - 1][c]) % MOD;
}
}
......
......@@ -430,6 +430,30 @@ def fix_bug():
def add_color_for_special_exercises():
dirs = ['data/2.算法中阶', 'data/3.算法高阶', 'data/1.算法初阶']
exercises_ids = []
for dir in dirs:
dirs_ = os.listdir(dir)
algo_floor_dirs = []
for algo_floor_dir in dirs_:
leetcode_class_dir = os.path.join(dir, algo_floor_dir)
if os.path.isdir(leetcode_class_dir):
algo_floor_dirs.append(leetcode_class_dir)
for algo_floor_dir in algo_floor_dirs:
exercises_dirs_ = os.listdir(algo_floor_dir)
exercises_dirs = []
for exercises_dir_ in exercises_dirs_:
exercises_dir = os.path.join(algo_floor_dir, exercises_dir_)
if os.path.isdir(exercises_dir):
exercises_dirs.append(exercises_dir)
for idx, tem_dir in enumerate(exercises_dirs):
config_path = os.path.join(tem_dir, 'config.json')
solution_md_path = os.path.join(tem_dir, 'solution.md')
print(solution_md_path)
......@@ -445,7 +469,7 @@ if helper_function == 'modify_back_up_dir_name':
fix_bug()
add_color_for_special_exercises()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册