跳到正文

题解

题解:P11825 [TOIP2024] 6174

Fall_Dream约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年3月1日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
获赞
4
题目
P11825

版本

  1. v1当前

模拟简单题。
思路:每次按照题目模拟,用 umap 之类的哈希表即可判环。(作者用的是 gp_hash_table)即可复杂度 O(n⋅d⋅stp)O(n\cdot d\cdot stp)。(stpstp 即 ∑c\sum c,由于题目保证 stp≤105stp\le 10^5 所以可以通过)。map 则多加个 log⁡stp\log stp。

代码

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define rep(i,l,r) for(auto i=l;i<=r;i++)
#define int long long
constexpr int inf=1e18,N=5e5+5,mod=998244353,NN=1005;
using namespace std;
using namespace __gnu_pbds;
gp_hash_table<int,int>h;
inline int work(int n,int d){//d 位的 n 按照数位升序排序
	static int dig[11],top=0,cnt[11];
	int res=0;
	rep(i,1,d) dig[++top]=n%10,n/=10;
	rep(i,1,top) cnt[dig[i]]++;
	top=0;
	rep(i,0,9)
		while(cnt[i])
			res=res*10+i,cnt[i]--;
	return res;
}
inline int rev(int x,int d){//反转
	int res=0;
	rep(i,1,d) res=res*10+x%10,x/=10;
	return res;
}
signed main(){
	int T,d,n,t;
	cin>>T>>d;
	while(T--){
		h.clear();
		cin>>n;
		while(!h[n]){//如果出现过则有环
			h[n]=1;//记录
			t=work(n,d);
			n=rev(t,d)-t;//最大 - 最小
		}
		cout<<n<<"\n";
	}
	return 0;
}
原文
存档评论1 条抓取于