Lang:G++
Edit123456789101112131415161718192021222324252627#include "stdio.h"int y,t,o;int a(int x){if(x==1)return 0;if(x<=y)return (a(x-1)+y)%x;t=a(x-x/y);o=x%y;if(t<o)return t-o+x;elsereturn t-o+(t-o)/(y-1);return 0;}int main(){int b,c;scanf("%d",&b);while(b--){scanf("%d %d",&c,&y);printf("%d\n",a(c));}return 0;}