Cantor expands:
#include <iostream> using namespace std; int board[10]{1,1,2,6,24,120,720,5040,40320,362880};//Factorial of 0-9 bool book[10]; //Whether the number of marks has already appeared int main(){ string s; cin>>s; int sum = 1; for(int i=0 ; i<s.length() ; i++){ int mid = s[i]-'0'; int t = 0; for(int j=1 ; j<mid ; j++){ if(book[j] == false)t++; } sum += t*board[s.length()-i-1]; book[mid] = true; } cout<<sum<<endl; return 0; }
Cantor's inverse expansion:
#include<iostream> using namespace std; int board[10]{1,1,2,6,24,120,720,5040,40320,362880};//Factorial of 0-9 bool book[10];//Whether the number of tags has appeared int main(){ string s; cin>>s; int N; cin>>N; N--; for(int i=s.length() ; i>=1 ; i--){ int mid = N/board[i-1]+1; for(int i=1 ; i<=mid ; i++){ if(book[i])mid++; } book[mid] = true; cout<<mid; N %= board[i-1]; } return 0; }