bzoj3301 康托展开

版权声明:转载注意打标记哦( •̀ ω •́ )y https://blog.csdn.net/qq_35866453/article/details/62044932

分析:其实就是裸的康托展开,当然还有逆向的。具体是啥建议看dalaoblog:http://www.cnblogs.com/Tunix/p/4511044.html

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e5+5;
typedef long long ll;
ll a[30];
int n,m;
ll fac[30];
bool vis[100];
inline void pailie(ll x)
{
    memset(vis,0,sizeof(vis));
    fo(i,1,n)
    {
        int t=x/fac[n-i];
        int j,k;
        for(k=1,j=0;j<=t;k++)
        if (!vis[k])j++;
        a[i]=k-1;
        vis[k-1]=1;
        x%=fac[n-i];
    }
    fo(i,1,n-1)printf("%d ",a[i]);
    printf("%d\n",a[n]);
}
inline void hanghao()
{
    ll ans=1;
    memset(vis,0,sizeof(vis));
    fo(i,1,n)
    {
        int j=0,k;
        vis[a[i]]=1;
        fo(k,1,a[i])
        if (!vis[k])j++;
        ans+=j*fac[n-i];
    }
    printf("%lld\n",ans);
}
int main()
{
    scanf("%d%d",&n,&m);
    fac[0]=1;
    fo(i,1,20)fac[i]=fac[i-1]*i;
    char ch[5];
    while (m--)
    {
        scanf("%s",ch);
        if (ch[0]=='P')
        {
            ll x;
            scanf("%lld",&x);
            x--;
            pailie(x);
        } 
        else 
        {
            fo(i,1,n)scanf("%lld",&a[i]);
            hanghao();
        }
    }
    return 0;
}
阅读更多
换一批

没有更多推荐了,返回首页