221 字
1 分钟
CF1988A-Split the Multiset 题解

CF1988A#

题意简述#

给定一个正整数 nn,每次可以将其拆分成 kk 个数,求将 nn 变为 nn11 的最少操作次数。

题目分析#

对于一个数 nn,我们每次可以将其拆分为 (k1)(k-1)11,和 11(nk)(n-k),所以只需要暴力枚举 nn 能被拆分几次,就可以得出答案,具体见下:

n=5,k=2n=5,k=2时:

  • 55
  • 1,41,4
  • 1,1,31,1,3
  • 1,1,1,21,1,1,2
  • 1,1,1,1,11,1,1,1,1

不难发现只需要每次将 nn 减去 k1k-1,一直减到 n<=1n<=1,此时的操作次数即为答案。

Code#

#pragma GCC optimize(1, 2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ONLINE_JUDGE
#define MULTI_CASES
const int MaxN = 2e5+100;
const int INF = 1e9;
int T=1, N, M;
int a[MaxN];
int ans=0;
inline void Solve()
{
cin>>N>>M;
if(N==1){
cout<<0<<endl;
return ;
}
ans=0;
for(int i=1;;i++){
N-=M-1;
// cout<<N<<endl;
ans++;
if(N<=1){
break;
}
}
cout<<ans<<endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
#ifdef MULTI_CASES
cin >> T;
while (T--)
#endif
Solve();
//fclose(stdin);
//fclose(stdout);
return 0;
}
CF1988A-Split the Multiset 题解
https://blog.introl.top/posts/cf1988a-split-the-multiset-题解/
作者
Introl
发布于
2024-07-16
许可协议
CC BY-NC-SA 4.0