QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#226781#7615. Sequence Foldingckiseki#WA 142ms91200kbC++202.3kb2023-10-26 16:09:382023-10-26 16:09:40

Judging History

你现在查看的是最新测评结果

  • [2023-10-26 16:09:40]
  • 评测
  • 测评结果:WA
  • 用时:142ms
  • 内存:91200kb
  • [2023-10-26 16:09:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
  cerr << "\e[1;32m[ " << s << " ] = [ ";
  for (int f = 0; L != R; ++f)
    cerr << (f ? ", " : "") << *L++;
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

const int maxn = 100025 * 60;
const int inf = 1e9;
int ch[maxn][2];
int cnt[maxn];
int dp[maxn][2];
int tot;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int64_t n;
  int m;
  cin >> n >> m;

  const int l = __lg(n);
  const auto F = [&](int64_t p) {
    for (int i = l - 1; i >= 1; i--) {
      if (p >> i & 1) {
        int64_t z = p >> i;
        p = (z << i) + ((z + 1) << i) - 1 - p;
      }
    }
    int64_t q = 0;
    for (int j = 0; j < l; j++) {
      q <<= 1;
      q |= p & 1;
      p >>= 1;
    }
    return q;
  };

  const auto Insert = [&](int64_t z) {
    int now = 0;
    for (int j = l - 1; j >= 0; j--) {
      int d = z >> j & 1;
      if (!ch[now][d]) {
        ch[now][d] = ++tot;
      }
      now = ch[now][d];
      ++cnt[now];
    }
  };

  const auto Dp = [&]() {
    const auto dfs = [&](auto self, int i) {
      const int now = i;
      debug(i, cnt[i]);
      if (!ch[now][0] && !ch[now][1]) {
        dp[i][0] = cnt[i] != 0;
        dp[i][1] = cnt[i] == 0;
        return;
      }

      if (ch[now][0]) {
        self(self, ch[now][0]);
        dp[i][0] += dp[ch[now][0]][0];
        dp[i][1] += dp[ch[now][0]][1];
      } else {
        dp[i][1] = inf;
      }

      if (ch[now][1]) {
        self(self, ch[now][1]);
        dp[i][0] += dp[ch[now][1]][0];
        dp[i][1] += dp[ch[now][1]][1];
      } else {
        dp[i][1] = inf;
      }

      dp[i][0] = min(dp[i][0], dp[i][1] + 1);
      dp[i][1] = min(dp[i][1], dp[i][0] + 1);

    };

    dfs(dfs, 0);
    return min(dp[0][0], dp[0][1]);
  };

  for (int i = 0; i < m; i++) {
    int64_t x;
    cin >> x;
    --x;
    x = F(x);
    debug(x);
    Insert(x);
  }

  cout << Dp() << '\n';

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5468kb

input:

8 3
1 5 8

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3452kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 80ms
memory: 41740kb

input:

17179869184 100000
138476 774165 993977 1152277 1236393 1244970 1534017 1693701 1763926 1778781 1842066 1874644 1885666 2120429 2485344 2977941 3158255 3340476 3504862 4000117 4066652 4197639 4338723 4389163 4576965 4761601 5097091 5175412 5295902 5810551 5855982 6001770 6111262 6163309 6351547 6582...

output:

99999

result:

ok single line: '99999'

Test #4:

score: 0
Accepted
time: 91ms
memory: 52616kb

input:

549755813888 100000
16886305 20807233 27844305 30727441 30898344 35755457 38085835 43336454 47877882 50347884 53237225 53718183 60030541 66954859 80773500 82511603 84025040 86398615 93070876 94502940 98906398 100677488 103720017 105522694 116741042 122492007 135222584 155167916 160926866 166110647 1...

output:

100000

result:

ok single line: '100000'

Test #5:

score: 0
Accepted
time: 122ms
memory: 61496kb

input:

17592186044416 100000
44842545 229248515 253567434 347949154 349195610 404810585 639421407 650796923 1019260054 1250861689 1315840401 1318619991 1339387462 1388173647 1406074815 1459749263 1707998226 1902480662 2060860604 2075157570 2410720375 2589192480 2742051226 2784829021 3019629868 3194189913 3...

output:

100000

result:

ok single line: '100000'

Test #6:

score: 0
Accepted
time: 130ms
memory: 72704kb

input:

562949953421312 100000
8468403039 19260915102 24550792804 45571277635 47757798888 50487845666 53656890708 58778712483 63838097144 65697633747 74717895118 75607193564 75790076603 82739180544 88493216722 90960251492 93191423725 93775335122 96870622706 97818052601 107098516035 116573978680 117388104977...

output:

100000

result:

ok single line: '100000'

Test #7:

score: 0
Accepted
time: 128ms
memory: 83144kb

input:

18014398509481984 100000
595466408158 695142884370 811588821663 938951385045 955148012821 1074515190235 1209454535782 1319295844076 1465300774125 1634202068435 1761189966958 2474372766317 2493877995320 2532743464849 2607093321941 2755490217777 3183921545337 3499339208003 3649317240659 3873577127103 ...

output:

100000

result:

ok single line: '100000'

Test #8:

score: 0
Accepted
time: 142ms
memory: 91200kb

input:

576460752303423488 100000
13970345269592 15376826852028 24802122999858 27223268306434 36702541028981 43837014560573 44921933577642 58096934157757 59667447677923 66975875846281 84770936584661 86367511887665 89865085383436 91951807720175 103815897231785 104261045426912 107706410045438 108769878131800 ...

output:

100000

result:

ok single line: '100000'

Test #9:

score: 0
Accepted
time: 10ms
memory: 9428kb

input:

65536 65536
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...

output:

0

result:

ok single line: '0'

Test #10:

score: -100
Wrong Answer
time: 18ms
memory: 10736kb

input:

131072 100000
1 2 4 5 6 9 11 12 14 15 17 18 19 21 22 23 24 26 28 30 31 32 33 34 37 39 40 41 43 44 45 46 47 48 49 51 52 53 54 55 56 57 61 62 64 65 66 67 68 69 70 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 88 89 90 91 92 93 96 97 98 100 102 103 104 105 106 107 108 109 110 111 112 113 115 116 117 118...

output:

45713

result:

wrong answer 1st lines differ - expected: '27315', found: '45713'