CSES - Datatähti 2019 loppu - Results
Submission details
Task:Binääripuu
Sender:Olli Järviniemi
Submission time:2019-01-17 13:45:55 +0200
Language:C++
Status:READY
Result:49
Feedback
groupverdictscore
#1ACCEPTED23
#2ACCEPTED26
#30
Test results
testverdicttimegroup
#1ACCEPTED0.19 s1details
#2ACCEPTED0.19 s1details
#3ACCEPTED0.18 s1details
#4ACCEPTED0.18 s1details
#5ACCEPTED0.18 s1details
#6ACCEPTED0.18 s1details
#7ACCEPTED0.18 s1details
#8ACCEPTED0.18 s1details
#9ACCEPTED0.18 s1details
#10ACCEPTED0.18 s1details
#11ACCEPTED0.18 s1details
#12ACCEPTED0.18 s1details
#13ACCEPTED0.19 s1details
#14ACCEPTED0.18 s1details
#15ACCEPTED0.18 s1details
#16ACCEPTED0.20 s1details
#17ACCEPTED0.19 s1details
#18ACCEPTED0.18 s1details
#19ACCEPTED0.18 s1details
#20ACCEPTED0.18 s1details
#21ACCEPTED0.18 s1details
#22ACCEPTED0.20 s1details
#23ACCEPTED0.19 s1details
#24ACCEPTED0.18 s1details
#25ACCEPTED0.18 s1details
#26ACCEPTED0.18 s1details
#27ACCEPTED0.18 s1details
#28ACCEPTED0.18 s1details
#29ACCEPTED0.18 s1details
#30ACCEPTED0.18 s1details
#31ACCEPTED0.18 s1details
#32ACCEPTED0.18 s1details
#33ACCEPTED0.18 s1details
#34ACCEPTED0.18 s1details
#35ACCEPTED0.19 s1details
#36ACCEPTED0.18 s2details
#37ACCEPTED0.19 s2details
#38ACCEPTED0.19 s2details
#39ACCEPTED0.19 s2details
#40ACCEPTED0.19 s2details
#41ACCEPTED0.18 s2details
#42ACCEPTED0.18 s2details
#43ACCEPTED0.19 s2details
#44ACCEPTED0.19 s2details
#45ACCEPTED0.18 s2details
#46ACCEPTED0.19 s3details
#47ACCEPTED0.18 s3details
#48ACCEPTED0.18 s3details
#49ACCEPTED0.19 s3details
#50ACCEPTED0.20 s3details
#51ACCEPTED0.19 s3details
#52ACCEPTED0.18 s3details
#53ACCEPTED0.18 s3details
#54ACCEPTED0.18 s3details
#55--3details
#56--3details
#57--3details
#58--3details
#59--3details
#60--3details
#61--3details
#62--3details
#63ACCEPTED0.35 s3details
#64--3details
#65--3details
#66--3details
#67ACCEPTED0.19 s3details
#68ACCEPTED0.18 s3details
#69ACCEPTED0.19 s3details
#70ACCEPTED0.19 s3details
#71ACCEPTED0.19 s3details
#72ACCEPTED0.18 s3details
#73ACCEPTED0.19 s3details
#74ACCEPTED0.18 s3details
#75ACCEPTED0.18 s3details
#76ACCEPTED0.18 s3details
#77ACCEPTED0.19 s3details

Compiler report

input/code.cpp: In function 'std::__cxx11::string add(std::__cxx11::string, std::__cxx11::string)':
input/code.cpp:30:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        if(i < a.length() && a[i] == '1') {
           ~~^~~~~~~~~~~~
input/code.cpp:33:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        if(i < b.length() && b[i] == '1') {
           ~~^~~~~~~~~~~~
input/code.cpp: In function 'std::__cxx11::string toBin(std::__cxx11::string)':
input/code.cpp:76:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < a.length(); i+=2) {
                    ~~^~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:154:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < v.size(); ++i) {
                    ~~^~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>


using namespace std;

typedef long long ll;
typedef pair<int, string> is;

vector<is> e;

const int C = 205;

string po[C][100]; //n*10^k in binary

string add(string a, string b) {
    //Add two binary strings together
    int m = max(a.length(), b.length());
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    
    
    string res = "";
    int plusOne = 0;
    for(int i = 0; i < m; ++i) {
       int A = 0;
       int B = 0;
       if(i < a.length() && a[i] == '1') {
           ++A;
       }
       if(i < b.length() && b[i] == '1') {
           ++B;
       }
       
       if(A+B+plusOne == 3) {
           plusOne = 1;
           res += "1";
       } else if (A+B+plusOne == 2) {
           plusOne = 1;
           res += "0";
       } else if (A+B+plusOne == 1) {
           plusOne = 0;
           res += "1";
       } else {
           res += "0";
       }
    }
    if(plusOne == 1) {
        res += "1";
    }
    reverse(res.begin(), res.end());
    return res;
}

void init() {
    po[0][1] = "1";
    for(int i = 1; i < C; ++i) {
        string a = po[i-1][1] + "000";
        string b = po[i-1][1] + "0";
        po[i][1] = add(a, b);
    }
    
    for(int i = 2; i < 100; ++i) {
        for(int j = 0; j < C; ++j) {
            po[j][i] = add(po[j][i-1], po[j][1]);
        }
    }
}

string toBin(string a) {
    reverse(a.begin(), a.end());
    if(a.size()%2 == 1) a+="0";
    string b = "";
    for(int i = 0; i < a.length(); i+=2) {
        int digit1 = (int) (a[i] - '0');
        int digit2 = (int) (a[i+1] - '0');
        int to = digit1 + 10*digit2;
        if(to == 0) continue;
        b = add(b, po[i][to]);
    }
    return b;
}

unordered_map<string, bool> ma[C][C];


const int M = 1e9 + 7;

ll exp(ll a, ll e) {
    if(e == 0) return 1;
    if(e%2 == 0) {
        ll c = exp(a, e/2);
        return (c*c)%M;
    }
    ll c = exp(a, e-1);
    return (a*c)%M;
}

ll powers[65]; //Powers of two

int main() {
    int n, m;
    cin >> n >> m;

    init();
  //  string a = "124331232131521";
  //  a = toBin(a);
    for(int i = 1; i <= m; ++i) {
        string a;
        cin >> a;
        if(a.length() >= 65) {
            throw exception();
        }
        
        a = toBin(a);
    
    //    a = add(a, toBin("132411"));
        e.push_back({a.length(), a});

    }
    sort(e.begin(), e.end());
    
    vector<string> v;

    for(int i = 0; i < m; ++i) {
        string s = e[i].second;
 
        int ind = 0;
        if(s[1] == '1') {
            ind = 1;
        }
        bool fo = false;
        while(s.length() >= 1) {
        
            if(ma[s.length()][ind][s]) {
                fo = true;
                break;
            }
            s.pop_back();
        }
        if(fo) continue;
        v.push_back(e[i].second);
        ma[e[i].second.length()][ind][e[i].second] = true;
    }
    for(int i = 0; i <= n; ++i) {
        powers[i] = exp(2, i);
    }
    
    ll le = powers[n-2];
    ll ri = powers[n-2];
    
    for(int i = 0; i < v.size(); ++i) {
        
        if(v[i][1] == '1') {
            //Right
            ri -= powers[n - v[i].length()];
            ri %= M;
        } else {
            //Left
            le -= powers[n-v[i].length()];
            le %= M;
        }
    }
    
    
    ll ans = 2*le*ri;
    ans %= M;
    if(ans < 0) {
        ans += M;
    }
    
    cout << ans << "\n";
    
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
4 3
10
5
13

correct output
12

user output
12

Test 2

Group: 1

Verdict: ACCEPTED

input
2 0

correct output
2

user output
2

Test 3

Group: 1

Verdict: ACCEPTED

input
2 1
2

correct output
0

user output
0

Test 4

Group: 1

Verdict: ACCEPTED

input
2 1
3

correct output
0

user output
0

Test 5

Group: 1

Verdict: ACCEPTED

input
2 2
2
3

correct output
0

user output
0

Test 6

Group: 1

Verdict: ACCEPTED

input
6 0

correct output
512

user output
512

Test 7

Group: 1

Verdict: ACCEPTED

input
6 1
29

correct output
448

user output
448

Test 8

Group: 1

Verdict: ACCEPTED

input
6 1
63

correct output
480

user output
480

Test 9

Group: 1

Verdict: ACCEPTED

input
6 10
50
63
56
5
...

correct output
132

user output
132

Test 10

Group: 1

Verdict: ACCEPTED

input
6 10
25
57
62
42
...

correct output
162

user output
162

Test 11

Group: 1

Verdict: ACCEPTED

input
6 10
14
26
34
42
...

correct output
126

user output
126

Test 12

Group: 1

Verdict: ACCEPTED

input
6 10
21
60
23
59
...

correct output
128

user output
128

Test 13

Group: 1

Verdict: ACCEPTED

input
6 10
8
30
32
16
...

correct output
0

user output
0

Test 14

Group: 1

Verdict: ACCEPTED

input
6 20
46
39
55
24
...

correct output
48

user output
48

Test 15

Group: 1

Verdict: ACCEPTED

input
6 25
36
23
27
33
...

correct output
4

user output
4

Test 16

Group: 1

Verdict: ACCEPTED

input
6 62
42
11
4
61
...

correct output
0

user output
0

Test 17

Group: 1

Verdict: ACCEPTED

input
6 32
62
38
57
35
...

correct output
0

user output
0

Test 18

Group: 1

Verdict: ACCEPTED

input
6 21
38
51
47
35
...

correct output
60

user output
60

Test 19

Group: 1

Verdict: ACCEPTED

input
6 31
59
15
3
26
...

correct output
0

user output
0

Test 20

Group: 1

Verdict: ACCEPTED

input
6 14
13
53
55
26
...

correct output
256

user output
256

Test 21

Group: 1

Verdict: ACCEPTED

input
5 10
5
17
21
7
...

correct output
6

user output
6

Test 22

Group: 1

Verdict: ACCEPTED

input
6 5
38
18
31
44
...

correct output
312

user output
312

Test 23

Group: 1

Verdict: ACCEPTED

input
3 0

correct output
8

user output
8

Test 24

Group: 1

Verdict: ACCEPTED

input
3 1
2

correct output
0

user output
0

Test 25

Group: 1

Verdict: ACCEPTED

input
3 1
3

correct output
0

user output
0

Test 26

Group: 1

Verdict: ACCEPTED

input
3 2
4
6

correct output
2

user output
2

Test 27

Group: 1

Verdict: ACCEPTED

input
3 2
4
7

correct output
2

user output
2

Test 28

Group: 1

Verdict: ACCEPTED

input
3 2
5
6

correct output
2

user output
2

Test 29

Group: 1

Verdict: ACCEPTED

input
3 2
5
7

correct output
2

user output
2

Test 30

Group: 1

Verdict: ACCEPTED

input
3 2
3
6

correct output
0

user output
0

Test 31

Group: 1

Verdict: ACCEPTED

input
3 3
4
5
6

correct output
0

user output
0

Test 32

Group: 1

Verdict: ACCEPTED

input
3 3
4
6
7

correct output
0

user output
0

Test 33

Group: 1

Verdict: ACCEPTED

input
3 4
4
5
6
7

correct output
0

user output
0

Test 34

Group: 1

Verdict: ACCEPTED

input
5 0

correct output
128

user output
128

Test 35

Group: 1

Verdict: ACCEPTED

input
5 1
14

correct output
96

user output
96

Test 36

Group: 2

Verdict: ACCEPTED

input
2 0

correct output
2

user output
2

Test 37

Group: 2

Verdict: ACCEPTED

input
3 0

correct output
8

user output
8

Test 38

Group: 2

Verdict: ACCEPTED

input
4 0

correct output
32

user output
32

Test 39

Group: 2

Verdict: ACCEPTED

input
23 0

correct output
92960636

user output
92960636

Test 40

Group: 2

Verdict: ACCEPTED

input
36 0

correct output
635008130

user output
635008130

Test 41

Group: 2

Verdict: ACCEPTED

input
42 0

correct output
993282280

user output
993282280

Test 42

Group: 2

Verdict: ACCEPTED

input
57 0

correct output
608377687

user output
608377687

Test 43

Group: 2

Verdict: ACCEPTED

input
58 0

correct output
433510734

user output
433510734

Test 44

Group: 2

Verdict: ACCEPTED

input
59 0

correct output
734042929

user output
734042929

Test 45

Group: 2

Verdict: ACCEPTED

input
60 0

correct output
936171702

user output
936171702

Test 46

Group: 3

Verdict: ACCEPTED

input
2 0

correct output
2

user output
2

Test 47

Group: 3

Verdict: ACCEPTED

input
2 1
2

correct output
0

user output
0

Test 48

Group: 3

Verdict: ACCEPTED

input
2 1
3

correct output
0

user output
0

Test 49

Group: 3

Verdict: ACCEPTED

input
2 2
2
3

correct output
0

user output
0

Test 50

Group: 3

Verdict: ACCEPTED

input
59 0

correct output
734042929

user output
734042929

Test 51

Group: 3

Verdict: ACCEPTED

input
60 0

correct output
936171702

user output
936171702

Test 52

Group: 3

Verdict: ACCEPTED

input
60 1
2

correct output
0

user output
0

Test 53

Group: 3

Verdict: ACCEPTED

input
60 1
576460752303423487

correct output
399775198

user output
399775198

Test 54

Group: 3

Verdict: ACCEPTED

input
60 1
1152921504606846975

correct output
667973450

user output
667973450

Test 55

Group: 3

Verdict:

input
60 100000
793144025126277478
493960657696318193
320344157019848454
997683950258055349
...

correct output
12813436

user output
(empty)

Test 56

Group: 3

Verdict:

input
60 100000
986789393060040220
494066507202684187
290830182157570659
210871095549437400
...

correct output
95910570

user output
(empty)

Test 57

Group: 3

Verdict:

input
60 100000
562299894758506682
731132428235172865
481500339045425690
208947005967360560
...

correct output
331624281

user output
(empty)

Test 58

Group: 3

Verdict:

input
60 100000
257846911019866261
803021202233459674
658685893766403373
874237705485665885
...

correct output
880725152

user output
(empty)

Test 59

Group: 3

Verdict:

input
60 100000
681388061028265547
990382999862855868
290694941402209648
491574111397234946
...

correct output
768320742

user output
(empty)

Test 60

Group: 3

Verdict:

input
60 100000
816968727394104576
879176301780517486
1075817811951859004
632046213104404023
...

correct output
111155168

user output
(empty)

Test 61

Group: 3

Verdict:

input
60 100000
406817930303406656
101704482575856800
406817930303412346
813635860606874021
...

correct output
295651662

user output
(empty)

Test 62

Group: 3

Verdict:

input
59 100000
234885934259928845
244746379456935555
40048493361902167
530369614866009610
...

correct output
78892926

user output
(empty)

Test 63

Group: 3

Verdict: ACCEPTED

input
16 65534
26355
15205
3420
22822
...

correct output
0

user output
0

Test 64

Group: 3

Verdict:

input
60 99999
1030879879260090869
925369330408563145
403438777989811739
321883235203598800
...

correct output
596899803

user output
(empty)

Test 65

Group: 3

Verdict:

input
60 100000
418885324976523921
418885324976523063
837770649953084829
104721331244134167
...

correct output
0

user output
(empty)

Test 66

Group: 3

Verdict:

input
43 100000
5497882741157
4163319796881
3705445016042
8647080351851
...

correct output
317037701

user output
(empty)

Test 67

Group: 3

Verdict: ACCEPTED

input
3 0

correct output
8

user output
8

Test 68

Group: 3

Verdict: ACCEPTED

input
3 1
2

correct output
0

user output
0

Test 69

Group: 3

Verdict: ACCEPTED

input
3 1
3

correct output
0

user output
0

Test 70

Group: 3

Verdict: ACCEPTED

input
3 2
4
6

correct output
2

user output
2

Test 71

Group: 3

Verdict: ACCEPTED

input
3 2
4
7

correct output
2

user output
2

Test 72

Group: 3

Verdict: ACCEPTED

input
3 2
5
6

correct output
2

user output
2

Test 73

Group: 3

Verdict: ACCEPTED

input
3 2
5
7

correct output
2

user output
2

Test 74

Group: 3

Verdict: ACCEPTED

input
3 2
3
6

correct output
0

user output
0

Test 75

Group: 3

Verdict: ACCEPTED

input
3 3
4
5
6

correct output
0

user output
0

Test 76

Group: 3

Verdict: ACCEPTED

input
3 3
4
6
7

correct output
0

user output
0

Test 77

Group: 3

Verdict: ACCEPTED

input
3 4
4
5
6
7

correct output
0

user output
0