hihocoder 1296 数论三·约瑟夫问题
http://hihocoder.com/problemset/problem/1296
/* *********************************************** Author :devil Created Time :2016/4/26 12:57:34 ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <stdlib.h> using namespace std; int solve(int n,int k) { if(n==1) return 0; if(n<k) { int ans=0; for(int i=2;i<=n;i++) ans=(ans+k)%i; return ans; } int ans=solve(n-n/k,k),tmp=n%k; if(ans<tmp) ans=ans-tmp+n; else ans=ans-tmp+(ans-tmp)/(k-1); return ans; } int main() { //freopen("in.txt","r",stdin); int t,n,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); printf("%d\n",solve(n,k)); } return 0; }