洛谷 - 康托展開與逆康托展開

題目傳送

洛谷 - 康托展開與逆康托展開
洛谷 - 康托展開與逆康托展開

局限性,

1.由于要處理階乘,當階乘太大的時候階乘數組會爆精度。

2.隻可以處理一個排列的字典序(就是全部字元都要不一樣)

AC代碼

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
char str[20];//存字典序的字元
ull f[20],arr[20],n;
ull cantor()//康托展開
{
    ull ans = 0;
    for(int i = 0;i < n;i++)
    {
        ull x = 0;
        for(int j = i+1;j < n;j++)
            if(str[i]-str[j] > 0)
                x++;
        ans += (x*f[n-i-1]);
    }
    return ans;
}
void incantor(ull k)//逆康托展開
{
    vector<ull> v;
    for(ull i = 1;i <= n;i++) v.push_back(i);
    for(ull i = 1;i < n;i++)//v數組的第一個元素位置是0,是以下面也要相應改變
    {
        ull x = k/f[n-i];
        arr[i] = v[x];
        v.erase(v.begin()+x);
        k %= f[n-i];
    }
    arr[n] = v[0];
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    cin >> n;f[1] = f[0] = 1;
    for(int i = 0;i < n;i++) cin >> str[i];
    for(int i = 2;i <= n;i++) f[i] = f[i-1]*i;//預處理階乘
    incantor(cantor()-1);
    for(int i = 1;i <= n;i++) cout << arr[i] << " ";//存這個字典序的上一個排列,還可以處理字母的字典序
}
           
數論,數學 闆子代碼 洛谷

Illegally bringing weapons into Tiananmen Square ended awkwardly

This story teaches us about the importance of respect for the law, teamwork, and self-reflection. On this night full of expectations and challenges, Sichuan policeman Gou and his colleagues had an impressive encounter.

Illegally bringing weapons into Tiananmen Square ended awkwardly

At first, they arrived in bustling Beijing full of curiosity and hope, but due to a momentary negligence, they had to experience an embarrassing incident at the Tiananmen police station. Although they are police officers from Sichuan, in places like the capital, everyone must comply with strict regulations, including a ban on carrying weapons. A pivotal moment in this story, the report of an alert Beijing aunt, triggered the whole incident.

Illegally bringing weapons into Tiananmen Square ended awkwardly

Although this incident was a difficult test for Gou and his team, they maintained a professional attitude, explained the situation, and also felt the understanding of the local police station. The incident underscored the importance of following local laws in all circumstances, no matter where you are.

Illegally bringing weapons into Tiananmen Square ended awkwardly

In the end, the arrival of the Sichuan director and director solved this embarrassing situation, and also established a bridge of friendship and respect among the police at the police station. The event underscored the importance of teamwork, no matter what difficulties are encountered, as long as the team works closely together, it can overcome challenges.

Illegally bringing weapons into Tiananmen Square ended awkwardly

The Sichuan police completed their mission with difficulty but tenacity, capturing the suspect and eventually returning to Sichuan. They learned a valuable lesson along the way, respecting laws and regulations wherever they are, while also experiencing the value of teamwork and responsibility. This experience became a valuable memory in their careers, underscoring the strength of the team and the importance of self-reflection.

Illegally bringing weapons into Tiananmen Square ended awkwardly

In Beijing, the police officers at the Tiananmen Police Station were also touched by the dedication and responsibility of these police officers from Sichuan. Despite their limited communication, the encounter was impressive, and they remembered the professionalism of these police officers and their ability to cope with difficulties.

Illegally bringing weapons into Tiananmen Square ended awkwardly