QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#26115#1835. Fancy FormulasSorting#AC ✓1663ms3748kbC++201.2kb2022-04-06 16:26:082022-04-29 02:57:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 02:57:43]
  • 评测
  • 测评结果:AC
  • 用时:1663ms
  • 内存:3748kb
  • [2022-04-06 16:26:08]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
template<class T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<class T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
#define all(x) (x).begin(), (x).end()

ll p, q;

ll fast_pow(ll base, ll exp){
    if(!exp) return 1ll;
    if(exp & 1) return base * fast_pow(base, exp - 1) % p;
    ll t = fast_pow(base, exp >> 1);
    return t * t % p;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> p >> q;
    while(q--){
        ll a, b, c, d;
        cin >> a >> b >> c >> d;

        ll sum = (a + b) % p;
        if((c + d) % p != sum){
            cout << "-1\n";
            continue;
        }
        if(a == c){
            cout << "0\n";
            continue;
        }

        for(ll k = 1; true; ++k){
            ll new_a = (1ll << k) % p * a % p;
            ll diff = (new_a - c + p) % p;
            // cout << diff << " diff" << endl;
            diff *= fast_pow(sum, p - 2);
            diff %= p;
            // cout << diff << " c" << endl;

            if(diff < (1ll << k)){
                cout << k << "\n";
                break;
            }
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 109ms
memory: 3660kb

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: 254ms
memory: 3584kb

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: 1588ms
memory: 3684kb

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: 1645ms
memory: 3748kb

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: 1663ms
memory: 3620kb

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: 1545ms
memory: 3748kb

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