题意:n个人排成一行,从第一个人开始,每个k个人报数,报到数的人被杀死,剩下的人重新排成一行再报数。一共q个询问,每次询问第qi个死的人是谁。
析:是一个约瑟夫的变形,我们要考虑子问题的问题同样编号是0-n-1,如果在某一轮,第 i 个人如果能取模 k 为0,那么这一轮他就会被干掉,如果不是
那么下一轮他就是编号为 i-i/k-1,然后再处理每一轮多少个人被干掉,就OK了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 3000000 + 10; const int mod = 1000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int dp[maxn], sum[maxn], f[maxn], t[maxn], ans[maxn]; int main(){ int T; cin >> T; while(T--){ int q; scanf("%d %d %d", &n, &m, &q); int nn = n, cnt = 1; while(nn){ sum[cnt] = sum[cnt-1]; sum[cnt++] += (nn-1) / m + 1; nn -= (nn-1) / m + 1; } memset(t, 0, sizeof t); for(int i = 0; i < n; ++i){ dp[i] = i % m ? dp[i-i/m-1] + 1 : 1; f[i] = ++t[dp[i]]; } for(int i = 0; i < n; ++i) ans[sum[dp[i]-1]+f[i]] = i; while(q--){ scanf("%d", &m); printf("%d\n", ans[m]+1); } } return 0; }