Bayan 2015 Contest Warm Up
A:水题,把对应位置和标号一一匹配上,然后每次k多少对应就去坐多少即可
B:其实只要找周围4条,看能不能形成一个环,因为如果不成环,肯定有一个点会到不了,如果成环,那么中间任意点,就可以到达
C:找出左上角的点,然后以一边为长,枚举另一边的长度,然后dfs一遍,dfs的过程利用容斥可以加速
D:记录下gcd,每次多一个数字,就和前面的数字都取gcd,然后合并掉,下次利用这些已有的gcd去做,然后合并的过程要利用到gcd的递减性质,就可以直接从头往后找即可
代码:
A:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int k;
string ans[6];
int x[35], y[35];
int main() {
ans[0] = "+------------------------+";
ans[1] = "|#.#.#.#.#.#.#.#.#.#.#.|D|)";
ans[2] = "|#.#.#.#.#.#.#.#.#.#.#.|.|";
ans[3] = "|#.......................|";
ans[4] = "|#.#.#.#.#.#.#.#.#.#.#.|.|)";
ans[5] = "+------------------------+";
x[0] = 1; y[0] = 1;
x[1] = 2; y[1] = 1;
x[2] = 3; y[2] = 1;
x[3] = 4; y[3] = 1;
int cnt = 3;
for (int i = 4; i < 34; i++) {
if ((i - 4) % 3 == 0) {
x[i] = 1; y[i] = cnt;
}
if ((i - 4) % 3 == 1) {
x[i] = 2; y[i] = cnt;
}
if ((i - 4) % 3 == 2) {
x[i] = 4; y[i] = cnt;
cnt += 2;
}
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++)
ans[x[i]][y[i]] = 'O';
for (int i = 0; i < 6; i++)
cout << ans[i] << endl;
return 0;
}
B:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int n, m;
char a[25], b[25];
bool judge(char up, char down, char left, char right) {
if (up == down) return false;
if (left == right) return false;
if (up == '<' && left == '^') return false;
if (down == '<' && left == 'v') return false;
if (up == '>' && right == '^') return false;
if (down == '>' && right == 'v') return false;
return true;
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s%s", a, b);
char up = a[0], down = a[n - 1];
char left = b[0], right = b[m - 1];
if (judge(up, down, left, right)) printf("YES\n");
else printf("NO\n");
return 0;
}
C:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define sum(x1,y1,x2,y2) (g[x2][y2] - g[x1 - 1][y2] - g[x2][y1 - 1] + g[x1 - 1][y1 - 1])
const int INF = 0x3f3f3f3f;
const int N = 1005;
int n, m, g[N][N], ans = INF;
char str[N][N];
int dfs(int x,int y,int wx,int wy) {
if(sum(x, y + 1, x + wx - 1, y + wy) == wx * wy) return wx + dfs(x, y + 1, wx, wy);
if(sum(x + 1, y, x + wx, y + wy - 1) == wx * wy) return wy + dfs(x + 1, y, wx, wy);
return 0;
}
int main() {
scanf("%d%d",&n,&m);
int flag = 0,px,py;
for(int i = 1; i <= n; i++) scanf("%s",str[i] + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
if(str[i][j] == 'X') {
if(!flag) {flag = 1; px = i; py = j;}
g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + 1;
}
else g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
}
int tmp,wx,wy;
for(tmp = px; str[tmp][py] == 'X'; tmp++);
wx = tmp - px;
for(int i = py; str[px][i] == 'X'; i++)
if(dfs(px, py, wx, i - py + 1) + wx * (i - py + 1) == g[n][m])
ans = min(ans, wx * (i - py + 1));
for(tmp = py; str[px][tmp] == 'X'; tmp++);
wy = tmp - py;
for(int i = px; str[i][py] == 'X'; i++)
if(dfs(px, py, i - px + 1, wy) + (i - px + 1) * wy == g[n][m])
ans = min(ans, (i - px + 1) * wy);
if (ans == INF) ans = -1;
printf("%d\n", ans);
return 0;
}
D:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, ll> pii;
const int N = 100005;
int a[N];
pii save[N];
map<int, ll> ans;
int n;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
void build() {
int top = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < top; j++) save[j].first = gcd(save[j].first, a[i]);
save[top++] = make_pair(a[i], 1LL);
int sn = 1;
for (int j = 1; j < top; j++) {
if (save[sn - 1].first == save[j].first) save[sn - 1].second += save[j].second;
else save[sn++] = save[j];
}
top = sn;
for (int j = 0; j < top; j++) {
ans[save[j].first] += save[j].second;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
build();
int q;
scanf("%d", &q);
while (q--) {
int x;
scanf("%d", &x);
printf("%lld\n", ans[x]);
}
return 0;
}