//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<char, null_type, less<char>, rb_tree_tag,tree_order_statistics_node_update>
//using namespace atcoder; //https://github.com/atcoder/ac-library
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define bit(n, k) ((n >> k) & 1)
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef pair<int, int> pii;
const int mod=1e9+7;
map<int,char> mp;
ordered_set st;
ll sum[27];
void solve(int cnt, ll k, int x=1, ll mul=26){
if(x>cnt)return;
if(x==cnt){
assert(k<=mul);
rep(i,0,k-1)st.erase(*st.begin());
mp[x]=*st.begin();
return;
}
if(cnt-x>=15){
mp[x]=*st.begin();
st.erase(st.begin());
solve(cnt,k,x+1,mul-1);
return;
}
ll A=1;
for(ll i=1, j=mul-1; i<=cnt-x; i++,j--)A=A*j;
int idx=0;
while(k>A){
idx++;
k-=A;
}
mp[x]=*st.find_by_order(idx);
st.erase(st.find_by_order(idx));
solve(cnt,k,x+1,mul-1);
}
void test_case(int tt){
string s; cin>>s;
int n=(int)s.length();
ll k; cin>>k;
vector<int> a(26,0);
int cnt=0;
for(char &c:s){
if(!a[c-'a']){
a[c-'a']=++cnt;
}
}
sum[0]=1;
for(ll i=26, j=1; j<=cnt; j++,i--){
if(j<=14)
sum[j]=(sum[j-1]*i);
else
sum[j]=((sum[j-1]%mod)*i)%mod;
}
cout<<sum[cnt]%mod<<'\n';
if(cnt<15 && k>sum[cnt]){
cout<<"OVER\n";
return;
}
for(char i='a'; i<='z'; i++)st.insert(i);
solve(cnt,k);
for(char &c:s){
cout<<mp[a[c-'a']];
}
cout<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin>>t;
rep(i, 1, t + 1)
{
test_case(i);
}
return 0;
}
Compilation message
JK.cpp: In function 'void test_case(int)':
JK.cpp:49:9: warning: unused variable 'n' [-Wunused-variable]
49 | int n=(int)s.length();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |