添加链接描述
先模拟前(n-m)/(m-1)*2
#include<bits/stdc++.h> using namespace std; const int N=1100; #define int long long typedef long long ll; signed main(){ int T; cin>>T; while(T--){ int n,m; ll res=0; cin>>n>>m; if(m==1&&n!=1){ puts("-1"); continue; } else if(m==1&&n==1){ puts("1"); continue; } if(n<=m){ puts("1"); } else { ll p=(n-m)/(m-1); ll res=p*2; ll now=p*(m-1); n-=now; while(n>0){ if(n<=m){ res++; break; } n-=m-1; res+=2; } // cout<<p<<endl; cout<<res<<endl; } } return 0; }