CSES - HIIT Open 2018 - Results
Submission details
Task:Find a Word
Sender:Wave of Technology
Submission time:2018-05-26 13:51:38 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.01 sdetails
#11ACCEPTED0.02 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.01 sdetails
#15ACCEPTED0.01 sdetails
#16ACCEPTED0.01 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:48:6: warning: unused variable 'rem' [-Wunused-variable]
   ll rem = 1LL << (2*n-2);
      ^~~
input/code.cpp:51:6: warning: unused variable 'offset' [-Wunused-variable]
   ll offset = 0;
      ^~~~~~
input/code.cpp:107:7: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
       if (p.first == ch) {
       ^~

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> PLL;

const ll INF = 1000000000000000LL;

ll n;

ll choose(ll n, ll k) {

  k = min(k, n-k);
  
  ll res = 1;
  for (ll i=0; i<k; i++) {
    res *= (n-i);
    res /= (i+1);
  }

  return res;
  
}

ll countrem(PLL p) {
  ll i = p.first;
  ll j = p.second;

  return choose(2*n-2-i-j, n-1-i);
}

int main() {

  cin.tie(NULL);
  std::ios::sync_with_stdio(false);

  cin >> n;

  ll k;
  cin >> k;

  vector<string> s (n);
  for (int i=0; i<n; i++) {
    cin >> s[i];
  }

  ll rem = 1LL << (2*n-2);


  ll offset = 0;

  vector<vector<bool> > trythis(n, vector<bool>(n, false) );  
  trythis[0][0] = true;
  
  vector<vector<ll> > count(n, vector<ll>(n, 0) );  
  count[0][0] = 1;
  
  string res;
  res += s[0][0];

  
  for (int step=0; step<2*n-2; step++) {

    vector<pair<char, PLL> > poss;

    //    cout << "String: " << res << " k " << k << endl;
    
    
    for (int i=0; i<n; i++) {
      int j = step-i;
      if (j<0 || j>=n) { continue; }

      if (!trythis[i][j] ) { continue; }

      if (i<n-1) {
	poss.push_back(make_pair(s[i+1][j], make_pair(i+1, j)));
	count[i+1][j] += count[i][j];
      }
      if (j<n-1) {
	poss.push_back(make_pair(s[i][j+1], make_pair(i, j+1)));
	count[i][j+1] += count[i][j];
      }
      
    }

    sort(poss.begin(), poss.end());

    auto it = unique(poss.begin(), poss.end());
    poss.resize(it - poss.begin());


    ll thiscum = 0;
    char ch;
    for (auto p : poss) {
      //      cout << " Test pos: " << p.second.first << " " << p.second.second << " rem " << countrem(p.second) << " count to this " << count[p.second.first][p.second.second] << endl;
      thiscum += countrem(p.second) * count[p.second.first][p.second.second];
      if (thiscum >= k) {
	ch = p.first;
	//	cout << " Char: " << ch << endl;
	break;
      }
    }
    
    bool isprev = true;
    for (auto p : poss) {
      if (p.first == ch) {
	trythis[p.second.first][p.second.second] = true;
	isprev = false;
      } else if (isprev) {
	k -= countrem(p.second) * count[p.second.first][p.second.second];
     }
    }

    res += ch;
    
  }

  cout << res << endl;

  return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
4 1
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 2

Verdict: ACCEPTED

input
4 2
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 3

Verdict: ACCEPTED

input
4 10
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 4

Verdict: ACCEPTED

input
4 19
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 5

Verdict: ACCEPTED

input
4 20
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 6

Verdict: ACCEPTED

input
4 1
QNJP
EVJU
XHZF
RXCV

correct output
QEVHXCV

user output
QEVHXCV

Test 7

Verdict: ACCEPTED

input
4 2
QNJP
EVJU
XHZF
RXCV

correct output
QEVHZCV

user output
QEVHZCV

Test 8

Verdict: ACCEPTED

input
4 10
QNJP
EVJU
XHZF
RXCV

correct output
QEXRXCV

user output
QEXRXCV

Test 9

Verdict: ACCEPTED

input
4 19
QNJP
EVJU
XHZF
RXCV

correct output
QNVJZCV

user output
QNVJZCV

Test 10

Verdict: ACCEPTED

input
4 20
QNJP
EVJU
XHZF
RXCV

correct output
QNVJZFV

user output
QNVJZFV

Test 11

Verdict: ACCEPTED

input
30 1
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 12

Verdict: ACCEPTED

input
30 15033633249770520
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 13

Verdict: ACCEPTED

input
30 30067266499541040
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 14

Verdict: ACCEPTED

input
30 1
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QNJLGFBBMJBHCCBOCEFBPLDCFGDJKB...

user output
QNJLGFBBMJBHCCBOCEFBPLDCFGDJKB...

Test 15

Verdict: ACCEPTED

input
30 15033633249770520
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QNXZYLYURTAYRYZQUKZCYOPKYKLQPX...

user output
QNXZYLYURTAYRYZQUKZCYOPKYKLQPX...

Test 16

Verdict: ACCEPTED

input
30 30067266499541040
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QRXZYLYURTAYRYZQUKZCYOPKYKLQPX...

user output
QRXZYLYURTAYRYZQUKZCYOPKYKLQPX...