Submission #1074103

#TimeUsernameProblemLanguageResultExecution timeMemory
10741038e7Tricolor Lights (JOI24_tricolor)C++17
76 / 100
118 ms26988 KiB
#include "Anna.h"
#include <bits/stdc++.h>
using namespace std;
namespace{
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);


ll po(ll a, ll n) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret = ret * a;
        n >>= 1;
        a = a * a;
    }
    return ret;
}
vector<int> adj[200000];
vector<int> build_path() {
    int n = 10;
    auto good = [&] (ll x) {
        for (int i = 0;i < n;i++) {
            if (x % 3 == 0) return true;
            x /= 3;
        }
        return false;
    };
    ll m = po(3, n);
    for (int i = 0;i < m;i++) {
        for (int j = 0;j < 3;j++) {
            int to = (i * 3 + j) % m;
            if (!good(i) && j != 0) continue;
            if (i == 0 && j == 0) continue;
            adj[i].push_back(to); 
        }
    } 
    vector<int> path, stk;
    int cur = 0;
    stk.push_back(cur);
    while (stk.size()) {
        cur = stk.back();
        int tmp = cur;
        while (adj[cur].size()) {
            int v = adj[cur].back();
            adj[cur].pop_back();
            cur = v;
            stk.push_back(v);
            break;
        }        
        if (tmp != cur) continue;
        path.push_back(cur);
        stk.pop_back();
    }
    reverse(path.begin(), path.end());
    return path;
}

int to_int(char c) {
    if (c == 'R') return 0;
    else if (c == 'G') return 1;
    else return 2;
}

char to_char(int i) {
    const string S = "RGB";
    return S[i];
}

int add_diff(int prv, int res) {
    if ((prv+1)%3 != res) return (prv+1)%3;
    else return (prv+2)%3;
}
void add_bit(std::vector<int> &ret, std::vector<int> &v, int bit) {
    //add semantic bit, could be 0/1/2, which takes up v[ind] and v[ind+1]
    int ind = ret.size();
    if (ind >= v.size()) return;
    if (ind + 1 >= v.size()) {
        ret.push_back(add_diff(ret.back(), v[ind]));
        return;
    }
    int done = 0;
    for (int i = 1;i < 3;i++) {
        for (int j = 1;j < 3;j++) {
            int vi = (v[ind] + i) % 3, vj = (v[ind+1] + j) % 3;
            if ((vj - vi + 3) % 3 == bit) {
                ret.push_back(vi);
                ret.push_back(vj);
                done = 1;
                break;
            }
        }
        if (done) break;
    } 
    ind = ret.size();
    if (ind >= v.size()) return;
    ret.push_back(add_diff(ret.back(), v[ind]));
}
}

pair<string, int> anna(int N, string S) {
    vector<int> v(N, 0), ret;
    for (int i = 0;i < N;i++) v[i] = to_int(S[i]);
    ret.push_back((v[0] + 1) %3);
    vector<int> nodes = build_path(); 
    vector<int> dig;
    for (int i = 0;i < 10;i++) add_bit(ret, v, 0), dig.push_back(0);
    for (int i = 1;i < nodes.size();i++) {
        if (ret.size() >= N) break;
        add_bit(ret, v, nodes[i] % 3);
        dig.push_back(nodes[i] % 3);
    } 
    pary(dig.begin(), dig.end());
    while (ret.size() >= N) ret.pop_back(); 
    
    int L = min(N, 34);
    string T;
    for (int i = 0;i < N;i++) T.push_back(to_char(ret[i]));
    return make_pair(T, L);
}
#include "Bruno.h"
#include <bits/stdc++.h>
using namespace std;
namespace{
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 500005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int N, L;
ll po(ll a, ll n) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret = ret * a;
        n >>= 1;
        a = a * a;
    }
    return ret;
}
vector<int> adj[200000];
vector<int> build_path() {
    int n = 10;
    auto good = [&] (ll x) {
        for (int i = 0;i < n;i++) {
            if (x % 3 == 0) return true;
            x /= 3;
        }
        return false;
    };
    ll m = po(3, n);
    for (int i = 0;i < m;i++) {
        for (int j = 0;j < 3;j++) {
            int to = (i * 3 + j) % m;
            if (!good(i) && j != 0) continue;
            if (i == 0 && j == 0) continue;
            adj[i].push_back(to); 
        }
    } 
    vector<int> path, stk;
    int cur = 0;
    stk.push_back(cur);
    while (stk.size()) {
        cur = stk.back();
        int tmp = cur;
        while (adj[cur].size()) {
            int v = adj[cur].back();
            adj[cur].pop_back();
            cur = v;
            stk.push_back(v);
            break;
        }        
        if (tmp != cur) continue;
        path.push_back(cur);
        stk.pop_back();
    }
    reverse(path.begin(), path.end());
    return path;
}
vector<int> path;
int edge_ind[maxn];

int to_int(char c) {
    if (c == 'R') return 0;
    else if (c == 'G') return 1;
    else return 2;
}
}

void init(int N, int l) {
    ::N = N;
    ::L = l;
    debug("L", l);
    path = build_path();
    for (int i = 1;i < path.size();i++) {
        int val = path[i-1] * 3 + path[i] % 3; 
        assert(edge_ind[val] == 0);
        edge_ind[val] = i;
    }
}

int bruno(string s) {
    if (N == L) return 1;
    assert(s.size() == L);
    vector<int> v[3];
    bool has_zero[3] = {0, 0, 0};
    for (int i = 1;i < L;i++) {
        int a = to_int(s[i]), b = to_int(s[i-1]);
        v[i%3].push_back((a - b + 3) % 3);
        if (a == b) has_zero[i%3] = 1;
    }
    int col = 0;
    for (int i = 0;i < 3;i++) {
        if (has_zero[i] && !has_zero[(i+1)%3]) col = i;
    } 

    assert(has_zero[0] || has_zero[1] || has_zero[2]);
    assert(!has_zero[0] || !has_zero[1] || !has_zero[2]);
    int num = 0;
    for (int i:v[col]) {
        num = num * 3 + i; 
    }
    int pos = -1 + 3 * edge_ind[num]; 
    pos += 1 - col;
    if (col == 0) pos -= 3;
    debug(num, edge_ind[num], col, pos);
    //debug(num, col);
    
    debug("Answer", pos);
    return pos;
}

Compilation message (stderr)

Anna.cpp: In function 'void {anonymous}::add_bit(std::vector<int>&, std::vector<int>&, int)':
Anna.cpp:91:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     if (ind >= v.size()) return;
      |         ~~~~^~~~~~~~~~~
Anna.cpp:92:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     if (ind + 1 >= v.size()) {
      |         ~~~~~~~~^~~~~~~~~~~
Anna.cpp:110:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     if (ind >= v.size()) return;
      |         ~~~~^~~~~~~~~~~
Anna.cpp: In function 'std::pair<std::__cxx11::basic_string<char>, int> anna(int, std::string)':
Anna.cpp:122:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for (int i = 1;i < nodes.size();i++) {
      |                    ~~^~~~~~~~~~~~~~
Anna.cpp:123:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |         if (ret.size() >= N) break;
      |             ~~~~~~~~~~~^~~~
Anna.cpp:14:19: warning: statement has no effect [-Wunused-value]
   14 | #define pary(...) 0
      |                   ^
Anna.cpp:127:5: note: in expansion of macro 'pary'
  127 |     pary(dig.begin(), dig.end());
      |     ^~~~
Anna.cpp:128:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  128 |     while (ret.size() >= N) ret.pop_back();
      |            ~~~~~~~~~~~^~~~

Bruno.cpp: In function 'void init(int, int)':
Bruno.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
Bruno.cpp:84:5: note: in expansion of macro 'debug'
   84 |     debug("L", l);
      |     ^~~~~
Bruno.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 1;i < path.size();i++) {
      |                    ~~^~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from Bruno.cpp:2:
Bruno.cpp: In function 'int bruno(std::string)':
Bruno.cpp:95:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   95 |     assert(s.size() == L);
      |            ~~~~~~~~~^~~~
Bruno.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
Bruno.cpp:117:5: note: in expansion of macro 'debug'
  117 |     debug(num, edge_ind[num], col, pos);
      |     ^~~~~
Bruno.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
Bruno.cpp:120:5: note: in expansion of macro 'debug'
  120 |     debug("Answer", pos);
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...