QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#719454#1835. Fancy FormulasmaspyAC ✓71ms3824kbC++236.3kb2024-11-07 01:19:392024-11-07 01:19:39

Judging History

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

  • [2024-11-07 01:19:39]
  • 评测
  • 测评结果:AC
  • 用时:71ms
  • 内存:3824kb
  • [2024-11-07 01:19:39]
  • 提交

answer

#line 1 "main.cpp"
#include <bits/stdc++.h>

#line 3 "/home/maspy/library/my_template.hpp"

using namespace std;

using ll = long long;
using ld = long double;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using vvvvvc = vector<vvvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define FOR3(i, m, n) for (ll i = (m); (i) < (ll)(n); ++(i))
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
#define FOR3_R(i, m, n) for (ll i = (ll)(n)-1; (i) >= (ll)(m); --(i))
#define FORIN(x, A) for (auto&& x : A)
#define FOR_nCk(s, n, k) \
  for (ll s = (1 << k) - 1, tmp_var = 0; s < (1 << n); \
  tmp_var = s | (s - 1), s = (tmp_var + 1) | (((~tmp_var & -~tmp_var) - 1) >> (__builtin_ctz(s) + 1))) 
#define all(x) x.begin(), x.end()

#define elif else if

#define popcnt __builtin_popcount

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}

template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

#define INT(...) \
  int __VA_ARGS__; \
  IN(__VA_ARGS__)
#define LL(...) \
  ll __VA_ARGS__; \
  IN(__VA_ARGS__)
#define STR(...) \
  string __VA_ARGS__; \
  IN(__VA_ARGS__)
#define CHR(...) \
  char __VA_ARGS__; \
  IN(__VA_ARGS__)
#define DBL(...) \
  long double __VA_ARGS__; \
  IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &a) {for(auto &i : a) scan(i);}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}

vi s_to_vi(string S, char first_char='a'){
  vi A(S.size());
  FOR(i, S.size()){
    A[i] = S[i] - first_char;
  }
  return A;
}

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
  os << A.fi << " " << A.se;
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
  for (size_t i = 0; i < A.size(); i++) {
    if(i) os << " ";
    os << A[i];
  }
  return os;
}

void print() {
  cout << "\n";
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(Tail)) cout << " ";
  print(forward<Tail>(tail)...);
}

const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }

template <typename T>
vector<T> cumsum(vector<T> A) {
  ll N = A.size();
  vector<T> B(N + 1);
  B[0] = T(0);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  return B;
}

vi bin_count(vi& A, ll size) {
  vi C(size);
  for (auto&& x : A) {
    ++C[x];
  }
  return C;
}

template <typename T>
vi argsort(vector<T>& A){
  vi ids(A.size());
  iota(all(ids), 0);
  sort(all(ids), [&](ll i, ll j){
    return A[i] < A[j] || (A[i] == A[j] && i < j);
  });
  return ids;
}


template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
#line 4 "main.cpp"

ll mpow(ll a, ll n, ll mod) {
  ll x = 1;
  while (n) {
    if (n & 1) x = x * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return x;
}

void solve() {
  LL(P, Q);
  auto calc = [&]() -> ll {
    LL(a, b, c, d);
    ll S = (a + b) % P;
    if ((c + d - S) % P != 0) return -1;

    ll rhs = a;
    ll inv_S = mpow(S, P - 2, P);
    FOR(n, 50) {
      // c + Sk = rhs を解く
      ll x = (rhs - c + P) % P;
      ll k = x * inv_S % P;
      if (k < (1 << n)) {
        return n;
      }
      rhs = 2 * rhs % P;
    }
    return -1;
  };

  FOR(_, Q) { print(calc()); }
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15);

  // auto T = in();
  // FOR(_, T) solve();

  solve();

  return 0;
}

詳細信息

Test #1:

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

input:

5 10
2 1 3 0
2 1 4 4
1 3 4 0
0 2 0 4
3 3 1 2
0 1 0 1
0 3 0 3
0 1 0 1
1 2 4 4
1 0 1 1

output:

2
1
2
-1
-1
0
0
0
1
-1

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 26ms
memory: 3628kb

input:

97 100000
30 56 74 12
95 39 8 29
11 42 76 74
48 63 58 53
74 22 85 11
80 23 84 4
30 90 30 90
92 91 41 45
21 82 11 92
65 30 28 67
74 57 95 36
16 31 78 66
2 77 6 73
83 20 41 62
45 44 92 94
96 28 77 47
76 12 87 1
47 80 42 85
46 91 65 72
23 39 4 58
21 96 37 80
83 33 66 50
84 21 61 44
4 78 47 35
39 50 39 ...

output:

6
6
5
6
6
-1
0
4
6
7
7
6
6
2
7
7
6
7
6
4
6
5
5
3
0
4
5
6
6
5
5
5
6
5
5
6
7
-1
5
4
-1
6
4
-1
4
6
5
5
-1
6
6
7
0
-1
2
-1
5
-1
5
7
2
4
6
4
6
6
-1
6
7
6
6
7
6
-1
4
2
7
0
6
-1
6
2
-1
4
6
5
-1
7
3
5
0
-1
7
3
4
6
4
6
0
1
5
7
6
-1
-1
-1
6
5
5
5
3
3
3
-1
-1
2
3
5
6
-1
-1
7
-1
5
7
6
5
6
-1
3
5
5
-1
4
5
6
-1
6...

result:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 34ms
memory: 3632kb

input:

1217 100000
1084 896 1095 885
1106 523 400 12
1200 37 911 326
992 1098 706 167
917 274 409 782
1154 689 530 96
738 563 879 422
389 1077 1034 432
415 735 61 1089
101 114 388 1044
512 255 818 1166
1149 620 663 1106
655 636 1036 255
86 922 154 854
848 990 740 1098
347 693 534 1192
521 126 866 68
235 36...

output:

9
8
9
7
10
11
8
9
8
9
10
9
10
9
10
-1
-1
10
8
10
10
8
11
10
10
7
-1
7
5
10
-1
9
10
9
8
9
6
10
-1
-1
10
8
9
10
7
9
-1
11
10
8
8
11
-1
-1
8
9
10
11
-1
9
-1
9
8
10
9
10
10
10
6
9
-1
10
10
10
6
10
10
9
10
9
-1
9
6
4
8
9
6
8
10
8
10
10
10
-1
10
10
9
-1
10
11
10
9
8
8
10
9
8
5
7
10
10
9
8
8
8
8
11
11
6
8
...

result:

ok 100000 numbers

Test #4:

score: 0
Accepted
time: 71ms
memory: 3824kb

input:

999999001 100000
283613712 224836068 452066136 56383644
830489448 158022048 357330405 631181091
920630113 770772345 401855596 289547861
264134671 92704742 146681100 226345536
879982866 896474292 569906689 421772490
229953446 13241605 620830189 622363863
704084273 156005177 727710355 132379095
352992...

output:

29
26
28
-1
-1
27
30
30
30
26
27
-1
29
28
24
-1
30
27
21
-1
28
30
30
30
29
24
-1
30
28
29
-1
29
26
28
29
29
29
29
-1
28
29
29
-1
30
-1
27
26
28
26
30
27
29
30
30
-1
26
30
22
27
30
30
28
29
26
29
29
28
29
28
28
29
25
29
25
25
29
29
27
28
29
28
29
-1
27
-1
-1
27
25
30
-1
27
30
30
29
28
29
29
29
28
28
...

result:

ok 100000 numbers

Test #5:

score: 0
Accepted
time: 68ms
memory: 3612kb

input:

999999323 100000
24495903 152638425 87217185 794250199
701443586 21540085 848139291 667628831
536695259 167429251 932417987 771705846
949269610 199487801 311910970 836846441
596115883 817268745 835693003 591405497
811816101 156369668 178128049 790057720
999725210 93460875 350432329 742753756
7750141...

output:

-1
-1
30
29
-1
30
27
30
28
25
30
29
29
30
-1
-1
27
28
-1
30
30
27
28
30
27
26
28
28
28
27
22
27
29
25
-1
-1
29
30
29
29
29
28
28
30
30
28
28
27
29
28
29
26
-1
30
28
27
30
30
-1
30
29
30
-1
29
29
30
29
29
29
26
29
29
30
30
30
30
27
29
30
-1
23
-1
28
27
-1
-1
30
-1
29
29
-1
-1
28
26
28
29
27
30
29
26
...

result:

ok 100000 numbers

Test #6:

score: 0
Accepted
time: 71ms
memory: 3560kb

input:

999999191 100000
920249851 263075456 910189304 273136003
259146706 440590130 217064118 394141081
606425484 261287223 359663798 711610714
633690165 59081357 129380510 563391012
703987751 364076553 981479539 86584765
767987403 851123667 887584320 731526750
634768660 684964613 976154077 343579196
63158...

output:

29
-1
-1
29
28
28
28
29
30
28
28
25
28
28
25
-1
-1
-1
26
28
30
30
30
-1
29
28
27
28
27
-1
-1
30
-1
30
26
30
-1
27
29
-1
-1
29
29
28
29
29
29
26
30
30
28
30
-1
26
28
29
28
26
27
27
-1
29
27
29
-1
23
29
30
25
26
27
27
30
30
30
-1
30
29
24
29
30
-1
28
29
29
29
28
30
24
26
30
30
29
28
27
29
28
28
29
29
...

result:

ok 100000 numbers

Test #7:

score: 0
Accepted
time: 70ms
memory: 3548kb

input:

1000000007 100000
94813842 786719232 320409267 561123807
612512900 492834218 649856203 487862838
217068316 128383196 301099606 501864265
161142637 582561998 449556540 294148095
641324809 704256785 280041480 991884267
907600109 846641568 611011100 143230570
550215399 866959290 347513960 69660722
6856...

output:

28
-1
-1
27
-1
30
29
29
-1
30
26
29
29
28
29
-1
30
-1
28
26
28
25
28
29
27
27
27
-1
30
30
30
29
30
29
28
28
29
29
-1
28
29
29
29
-1
30
29
28
28
-1
25
28
30
27
28
30
30
-1
29
27
-1
29
29
27
28
28
25
-1
29
-1
30
28
27
29
29
27
29
24
29
29
29
29
-1
25
-1
28
-1
28
29
28
25
29
30
30
29
27
-1
30
30
-1
29
...

result:

ok 100000 numbers