首页 > 技术文章 > BZOJ1970 [Ahoi2005] 矿藏编码

y-clever 2017-10-10 12:49 原文

Description

依次对每份进行编码,得S1,S2,S3,S4。该矿藏区的编码S为2S1S2S3S4。 例如上图中,矿藏区的编码为:2021010210001。 小联希望你能根据给定的编码统计出这片矿藏区一共有多少格子区域是平地。

Input

第一行有一个整数K,表示有矿藏区的规模为 (1 < K < 50)。第二行有一串编码,有0、1组成,长度不超过200,表示该矿藏区的编码。

Output

单行输出一个整数,表示矿藏区中一共有多少格子是平地。

Sample Input

2
2021010210001

Sample Output

9

题解

这题挺简单。基本上写一个高精度就能解决。

代码:

#include <cctype>
#include <cstdio>
const int K = 105;
int A[100];
int a[K];
inline int get() {
  int c;
  while (!isdigit(c = getchar()));
  return c - '0';
}
void solve(int dep) {
  int t = get();
  if (t == 0)
    ++a[dep * 2];
  else if (t == 2) {
    solve(dep - 1);
    solve(dep - 1);
    solve(dep - 1);
    solve(dep - 1);
  }
}
int main() {
  int k;
  scanf("%d", &k);
  solve(k);
  int len = 1;
  for (int i = 0; i < k * 2; ++i) {
    a[i + 1] += a[i] / 2;
    a[i] %= 2;
  }
  for (int i = k * 2; ~i; --i) {
    int t;
    for (int j = t = 0; j < len; ++j) {
      t = (A[j] = A[j] * 2 + t) / 10;
      A[j] %= 10;
    }
    if (t) A[len++] = t;
    if (a[i]) ++A[0];
  }
  while (len--) putchar(A[len] + '0');
  putchar('\n');
  return 0;
}

 ps:上微机课写题好爽。拿notepad(没有++)写题好有趣。

推荐阅读