题目链接:link
A - Carryless Square Root
思路:
两个数字进行题目说的运算,的出来的长度一定是奇数,因为 a ⋅ a = n a·a=n a⋅a=n a a a的长度为 l e n len len话,最终结果的长度为 2 ∗ l e n − 1 2*len-1 2∗len−1,把运算展开就可以的到这个结论,然后从高位到低位,从小数开始试, d f s dfs dfs
#include <bits/stdc++.h>
using namespace std;
const int N = 666;
string s;
int a[N], res[N], sum[N];
int len;
bool flag;
bool check(int pos) {
memset(sum, 0, sizeof(sum));
for (int i = 0; i <= pos; i++) {
for (int j = 0; j <= pos; j++) {
sum[i + j] = sum[i + j] + (res[i] * res[j]) % 10;
sum[i + j] %= 10;
}
}
if (pos == len - 1) {
pos = s.size() - 1;
}
for (int i = 0; i <= pos; i++) {
if (sum[i] != a[i]) {
return 0;
}
}
return 1;
}
void dfs(int pos) {
if (pos == len) {
for (int i = 0; i < pos; i++) {
cout << res[i];
}
cout << endl;
flag = 1;
exit(0);
}
for (int i = 0; i < 10; i++) {
res[pos] = i;
if (check(pos)) {
dfs(pos + 1);
}
}
}
int main() {
cin >> s;
len = s.size();
if (len % 2 == 0) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < len; i++) {
a[i] = s[i] - '0';
}
len = (len + 1) / 2;
dfs(0);
if (flag == 0) {
cout << -1 << endl;
}
}
I - Maze Connect
思路:
有些空间,例如空白的空地,在原图中是没有坐标的,所以不好判断图的封闭性,但是把原图放大为原来的 2 2 2倍后,再按照原图的相对位置,把图案还原,就能发现无论是空地还是墙壁都是有坐标的,然后直接 d f s dfs dfs搜索,搜有多少个封闭空间
#include <bits/stdc++.h>
using namespace std;
const int N = 6666;
char s[N];
int n, m;
int g[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void dfs(int x, int y) {
g[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 1 || nx > 2 * n || ny < 1 || ny > 2 * m) continue;
if (g[nx][ny]) continue;
dfs(nx, ny);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++) {
if (s[j] == '/') {
int x = 2 * i, y = 2 * j;
g[x][y - 1] = 1;
g[x - 1][y] = 1;
} else if (s[j] == '\\') {
int x = 2 * i, y = 2 * j;
g[x - 1][y - 1] = 1;
g[x][y] = 1;
}
}
}
for (int i = 1; i <= 2 * m; i++) {
if (!g[1][i]) {
dfs(1, i);
}
if (!g[2 * n][i]) {
dfs(2 * n, i);
}
}
for (int i = 1; i <= 2 * n; i++) {
if (!g[i][1]) {
dfs(i, 1);
}
if (!g[i][2 * m]) {
dfs(i, 2 * m);
}
}
int res = 0;
for (int i = 1; i <= 2 * n; i++) {
for (int j = 1; j <= 2 * m; j++) {
if (g[i][j] == 0) {
dfs(i, j);
res++;
}
}
}
cout << res << endl;
}
To be continued
如果你有任何建议或者批评和补充,请留言指出,不胜感激