QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118071#5424. NaN in a Heaphos_lyricAC ✓83ms3764kbC++146.2kb2023-07-03 01:36:302023-07-03 01:36:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-03 01:36:33]
  • 评测
  • 测评结果:AC
  • 用时:83ms
  • 内存:3764kb
  • [2023-07-03 01:36:30]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 1'000'000'007;
using Mint = ModInt<MO>;


Int N;

int H;
Int as[110], bs[110], cs[110];
Int ps[110], qs[110], rs[110];

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%lld", &N);
    
    H = 0;
    bs[0] = N;
    for (; bs[H] > 1; ) {
      ++H;
      bs[H] = bs[H - 1] >> 1;
    }
    reverse(bs, bs + (H + 1));
    as[0] = 1;
    cs[0] = 1;
    for (int i = 1; i <= H; ++i) {
      as[i] = as[i - 1] << 1;
      cs[i] = cs[i - 1] << 1 | 1;
    }
    
    ps[H] = 1;
    qs[H] = 1;
    rs[H] = 0;
    for (int i = H; --i >= 0; ) {
      ps[i] = 1 + 2 * ps[i + 1];
      if (bs[i + 1] & 1) {
        qs[i] = 1 + ps[i + 1] + qs[i + 1];
      } else {
        qs[i] = 1 + qs[i + 1] + rs[i + 1];
      }
      rs[i] = 1 + 2 * rs[i + 1];
    }
/*
cerr<<"as = ";pv(as,as+H+1);
cerr<<"bs = ";pv(bs,bs+H+1);
cerr<<"cs = ";pv(cs,cs+H+1);
cerr<<"ps = ";pv(ps,ps+H+1);
cerr<<"qs = ";pv(qs,qs+H+1);
cerr<<"rs = ";pv(rs,rs+H+1);
*/
    
    Mint base = 1;
    for (int i = 0; i <= H; ++i) {
      base *= Mint(ps[i]).pow(bs[i] - as[i]);
      base *= Mint(qs[i]);
      if (i < H) {
        base *= Mint(rs[i]).pow(cs[i] - bs[i]);
      }
    }
// cerr<<"base = "<<base<<endl;
    
    auto get = [&](int i, Int u) -> Int {
      if (u < bs[i]) return ps[i];
      if (u == bs[i]) return qs[i];
      if (u > bs[i]) return rs[i];
      assert(false);
    };
    auto solve = [&](int i, Int u) -> Mint {
      Mint numer = 1, denom = base;
      const Int sz = get(i, u);
      Int v = u;
      for (int j = i; --j >= 0; ) {
        v >>= 1;
        const Int t = get(j, v);
        numer *= t;
        denom *= (t - sz);
      }
      numer *= sz;
// cerr<<"solve "<<i<<" "<<u<<": "<<numer<<"/"<<denom<<endl;
      return numer / denom;
    };
    
    Mint ans = 0;
    for (int i = 0; i <= H; ++i) {
      {
        Int bef = bs[i];
        for (int j = i; --j >= 0; ) {
          const Int u = bs[j] << (i - j);
          if (bef > u) {
            ans += Mint(bef - u) * solve(i, u);
          }
          bef = u;
        }
      }
      
      ans += solve(i, bs[i]);
      
      if (i < H) {
        Int bef = bs[i];
        for (int j = i; --j >= 0; ) {
          const Int u = ((bs[j] + 1) << (i - j)) - 1;
          if (bef < u) {
            ans += Mint(u - bef) * solve(i, u);
          }
          bef = u;
        }
      }
    }
    ans /= N;
    printf("%u\n", ans.x);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3764kb

input:

5
1
3
7
10
20221218

output:

1
666666672
55555556
596445110
3197361

result:

ok 5 number(s): "1 666666672 55555556 596445110 3197361"

Test #2:

score: 0
Accepted
time: 7ms
memory: 3764kb

input:

1000
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
100
101...

output:

1
1
666666672
375000003
633333338
97222223
55555556
822222228
123456791
596445110
229888169
878681664
673617560
436681157
699563287
68610901
902106812
308824953
904504598
779800169
693423362
328728506
466956451
68896808
88594095
156207594
533144330
758445723
92289701
206321076
267778621
266415260
48...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 83ms
memory: 3620kb

input:

1000
1000000000
999999999
999999998
999999997
999999996
999999995
999999994
999999993
999999992
999999991
999999990
999999989
999999988
999999987
999999986
999999985
999999984
999999983
999999982
999999981
999999980
999999979
999999978
999999977
999999976
999999975
999999974
999999973
999999972
9999...

output:

191769339
839078655
63430702
488796230
588110810
163742647
964465260
961862258
425060141
344042065
747463620
143548999
281463738
797756640
382302841
365802993
511891059
902367958
70796927
495040138
33561329
728278059
879674300
992542203
309248753
251669085
188046077
672990625
635281273
113409431
972...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 81ms
memory: 3656kb

input:

1000
524125987
923264237
374288891
535590429
751244358
124321145
232930851
266089174
543529670
773363571
319728747
580543238
582720391
468188689
490702144
598813561
138628383
284660056
733781508
155605777
931759705
245485733
723534730
257812292
794937524
596788519
188451996
981010588
14483682
592676...

output:

726166876
355333772
482635633
758157044
775831523
760255234
748551027
477359472
86466892
226497967
994190156
546377096
39059573
537362710
398171443
66921908
845526053
340839799
621258613
555318917
603009173
528685604
550082786
978381020
650853592
125984432
139054932
319259349
481246666
178000233
799...

result:

ok 1000 numbers