#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef long double ld;
const ll INF=1000000000000000000;
const ll N=30+5;
ll n,m;
ll k;
ll go[N][N];
string v[N];
ll e[N][N];
vector<pair<ll,ll>>p[2*N];
ll t[100];
ll add(ll a,ll b)
{
a+=b;
if(a>INF)
{
a=INF;
}
return a;
}
ll mul(ll a,ll b)
{
if(a==0 || b==0)
{
return 0;
}
ll ans=a*b;
if(ans%a!=0 || ans/a!=b)
{
return INF;
}
if(ans>INF)
{
return INF;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(ll i=n;i>=1;i--)
{
for(ll j=m;j>=1;j--)
{
if(i==n && j==m)
{
go[i][j]=1;
}
else
{
go[i][j]=go[i+1][j]+go[i][j+1];
}
}
}
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
ll s=i+j-1;
p[s].push_back({i,j});
}
}
for(ll i=1;i<=n;i++)
{
cin>>v[i];
v[i]="."+v[i];
}
cin>>k;
e[1][1]=1;
string ans;
ans+=v[1][1];
for(ll di=2;di<=n+m-1;di++)
{
for(auto &it:p[di-1])
{
ll r=it.first;
ll c=it.second;
e[r+1][c]=add(e[r+1][c],e[r][c]);
e[r][c+1]=add(e[r][c+1],e[r][c]);
}
for(ll i=0;i<26;i++)
{
t[i]=0;
}
for(auto &it:p[di])
{
ll r=it.first;
ll c=it.second;
ll f=v[r][c]-'a';
ll a=e[r][c];
ll b=go[r][c];
t[f]=add(t[f],mul(a,b));
}
char ch;
ll cur=0LL;
for(ll i=0;i<26;i++)
{
cur+=t[i];
if(cur<k) continue;
cur-=t[i];
k-=cur;
ch=i+'a';
break;
}
for(auto &it:p[di])
{
ll r=it.first;
ll c=it.second;
if(v[r][c]!=ch)
{
e[r][c]=0;
}
}
ans+=ch;
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
3 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
560 KB |
Output is correct |
9 |
Correct |
2 ms |
560 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
636 KB |
Output is correct |
18 |
Correct |
2 ms |
636 KB |
Output is correct |
19 |
Correct |
2 ms |
636 KB |
Output is correct |
20 |
Correct |
4 ms |
636 KB |
Output is correct |