跳到正文

题解

题解:CF1443E Long Permutation

perD约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年9月29日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
题目
CF1443E

版本

  1. v2当前
  2. v1

Solution

注意到 ∑x≤2×1010\sum x \le 2 \times 10^{10} 且 14!>2×101014!>2\times10^{10}。

排列改变的次数不超过 ∑x\sum x,那么改变的一定只有最后的 1414 位。考虑维护前缀和暴力修改即可。

Code

C++
/* ChongYun */
#include<bits/stdc++.h>
#define START_LL 1
using namespace std;
#define start(x) using namespace x
#if START_LL
#define int long long
#endif
namespace IO{
    inline int read(){
        register int x=0,f=1;
        register char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-') f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=(x<<1)+(x<<3)+(ch^48);
            ch=getchar();
        }
        return x*f;
    }
    inline void write(int x){
    	if(x<0) x=-x,putchar('-');
        if(x>9) write(x/10);
        putchar((x%10)^48);
        return ;
    }
}start(IO);
const int N=2e5+5;
int n,q,cnt=1;
int fac[N],sum[N];
bool vis[N];
signed main(){
	n=read(); q=read();
	fac[0]=1;
	for(int i=1;i<=n;++i) fac[i]=fac[i-1]*i;
	for(int i=1;i<=n;++i) sum[i]=sum[i-1]+i;
	while(q--){
		int op=read();
		if(op==1){
			int l=read(),r=read();
			write(sum[r]-sum[l-1]),putchar('\n');
		}else{
			cnt+=read();
			int u=cnt;
			int pos=max(1ll,n-13);
			for(int i=pos;i<=n;++i) vis[i]=0;
			for(int i=pos;i<=n;++i){
				int now=0;
				for(int j=pos;j<=n;++j){
					if(vis[j]) continue;
					if(now+fac[n-i]>=u){
						u-=now,vis[j]=1;
						sum[i]=sum[i-1]+j;
						break;
					}
					else now+=fac[n-i];
				}
			}
		}
	}
	return 0;
}
原文