8.27 模拟赛总结
估分:
预估:
T1:100, T2:30, T3:0, T4:0.
实际:
T1:90, T2:30, T3:0, T4:0.
原因分析:
T1: 题目中 ,但我没有写快读,导致常数过大,导致 #19 和 #20 TLE ; T2:无; T3:无; T4:无。
我的题解
T1(0的个数):
思路:
若一个数 末尾有 个0,则一定能够写成 的形式,其中 为十进制整数。 由于 ,因此考虑统计 个数中2的因子数和5的因子数。因为2和5需要两两配对,因此取2的因子数和5的因子数的最小值就是答案。
由于本题读入数据量较大,因此需要快读。 时间复杂度:
代码:
#include <bits/stdc++.h>
using namespace std;
int n, cnt1, cnt2;
int cal(int x, int y) //计算因子个数
{
int ans = 0;
while (x % y == 0) ans++, x /= y;
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);// 一定加快读!!!
cin >> n;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
cnt1 += cal(x, 2);
cnt2 += cal(x, 5);
}
cout << min(cnt1, cnt2);
return 0;
}T2(吃月饼):
思路:
按照每一轮的报数顺序为这些人编号,在第一轮开始前,有:
第1轮完成后,有:
这时,我们重新将这些人按照这一轮的报数顺序编号,有:
若完成一轮报数后的原始序列为 ,重新编号后为 ,则显然有: (由于编号从1开始,截至到n,因此需要在取模前减1,再在取模后加1)
显然,Y的长度为,因此考虑递归。
将 视作新的 , 视作 ,得到递归式:
同时,当没有人时,也没有人吃月饼,因此:
综上,有:
时间复杂度:
代码:
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f(int i)// 递归函数
{
if (i == 0) return 0;
return (f(i - 1) + m - 1) % i + 1;
}
int main()
{
cin >> n >> m;
cout << f(n);
return 0;
}T3(黑洞):
思路:
首先考虑暴力的写法: 暴力很简单。枚举所有可能的点,运用搜索的方式,枚举所有可能的移动棋盘的方式,每次移动后检查剩余的棋子数。如果最后可以剩0个,就让答案加1。
但是,这样做的时间复杂度是 ,会超时。
所以我们尝试对暴力进行优化
容易发现棋子在连通块中可以自由移动(废话)。因此,如果连通块中有一个棋子可以成为天选之棋子,就意味着连通块中其他的棋子可以通过先移动到这个棋子的位置,再用这个棋子的移动方式来成为天选之棋子。
所以,我们只要先处理出点所在的连通块,同时处理出连通块中的棋子数,就只要一个连通块处理一次。
如果这个点不在之前访问过的连通块中,就使用漫水填充法,为当前点所在的连通块染色。同时,顺便处理出当前点可以进行的所有移动(多次移动后的总位移)。
然后,再判断其他点在进行这些运动后是否可以全部被清除,如果是,就加上当前点
这样就完成了。
代码:
#include <bits/stdc++.h>
using namespace std;
struct pos
{
int x, y;
};
int T, n, m, tag[45][45], d[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}, cnt, nodecnt, ans;
char c[45][45];
vector<pos> v[1605];
void bfs(int x, int y, int w, int nowx, int nowy)
{
queue<pos> q;
q.push({x, y});
tag[x][y] = w;
while (!q.empty())
{
x = q.front().x, y = q.front().y;
q.pop();
nodecnt++;
for (int i = 0; i < 4; i++)
{
int tar_x = x + d[i][0], tar_y = y + d[i][1];
if (tag[tar_x][tar_y] || c[tar_x][tar_y] != '.') continue;
q.push({tar_x, tar_y});
tag[tar_x][tar_y] = tag[x][y];
v[cnt].push_back({tar_x - nowx, tar_y - nowy});
}
}
return ;
}
bool try_clear()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (c[i][j] == '.' && tag[i][j] != cnt)
{
bool cl = 0;
for (pos p : v[cnt])
{
if (c[i + p.x][j + p.y] != '.')
{
cl = 1;
break;
}
}
if (!cl) return 0;
}
}
}
return 1;
}
int main()
{
cin >> T;
while (T--)
{
memset(c, 0, sizeof(c));
memset(tag, 0, sizeof(tag));
ans = cnt = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++) cin >> c[i][j];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (!tag[i][j] && c[i][j] == '.')
{
cnt++;
nodecnt = 0;
bfs(i, j, cnt, i, j);
if (try_clear()) ans += nodecnt;
}
}
}
cout << ans << '\n';
for (int i = 0; i <= cnt; i++) v[i].clear();
}
return 0;
}
T4(遍历):
思路:
根据007遍历迷宫的方式可以看出:树形迷宫的每个子树都是连续的一段。因此可以考虑进行区间动规。
状态定义:
表示 区间中可能有的树的数量。
状态转移方程:
首先,由于007在遍历完一个节点的所有子树后会返回到这个节点,所以,如果一个区间 可以还原成一个树,这个区间就必定满足 。 于是可以得出:
接下来,枚举断点 ,发现 两侧的子树可以进行组合,因此得出:
注意:为保证区间 的子树也是合法的树,因此需要额外判断 ,满足时再进行累加。
综上,整体的方程为:
边界:
显然的,有:
答案:
由状态定义可以得出:
时间复杂度:
代码:
#include <bits/stdc++.h>
using namespace std;
string s;
int f[305][305], n;
int main()
{
cin >> s;
n = s.length();
for (int i = 1; i <= n; i++) f[i][i] = 1;
for (int i = n; i >= 1; i--)
{
for (int j = i + 1; j <= n; j++)
{
if (s[i - 1] != s[j - 1]) f[i][j] = 0;
else
{
f[i][j] = f[i + 1][j - 1];
for (int k = i + 1; k <= j - 1; k++) f[i][j] += (s[k - 1] == s[i - 1]) * f[i + 1][k - 1] * f[k][j];
}
}
}
cout << f[1][n];
return 0;
}