【题目来源】杭电OJ2211
【题目链接】杭电2211杀人游戏
【代码】
#include <iostream>
#include <cstdio>
using namespace std;
long long hash[120][100000];
void printHash()
{
for(int i = 0; i < 8; i++){
printf("hash[%d] = ",i);
for(int j = 0; j < 10; j++){
cout<<hash[i][j]<<" ";
}
cout<<endl;
}
}
int main() {
int i,j;
long long lmax= 1;
lmax=lmax<<32;
for (i=3; i<110; ++i) {
hash[i][0]=i;
for (j=1; hash[i][j-1]<lmax; ++j) {
if (hash[i][j-1]%(i-1)==0) {
hash[i][j]=hash[i][j-1]/(i-1)*i-1;
} else {
hash[i][j]=(double)(hash[i][j-1]*i)/(double)(i-1);
}
}
}
//printHash();
int T;
cin>>T;
while (T--) {
int N,K;
cin>>N>>K;
int i;
for (i=0;; ++i) {
if (hash[K][i]>N) {
break;
}
}
cout<<hash[K][i-1]<<endl;
}
return 0;
}