题目链接:
Death SequenceTime Limit: 16000/8000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Now the problem is much easier: we have N men stand in a line and labeled from 1 to N, for each round, we choose the first man, the k+1-th one, the 2*k+1-th one and so on, until the end of the line. These poor guys will be kicked out of the line and we will execute them immediately (may be head chop, or just shoot them, whatever), and then we start the next round with the remaining guys. The little difference between the Romans and us is, in our version of story, NO ONE SURVIVES. Your goal is to find out the death sequence of the man.
For example, we have N = 7 prisoners, and we decided to kill every k=2 people in the line. At the beginning, the line looks like this:
1 2 3 4 5 6 7
after the first round, 1 3 5 7 will be executed, we have
2 4 6
and then, we will kill 2 6 in the second round. At last 4 will be executed. So, you need to output 1 3 5 7 2 6 4. Easy, right?
But the output maybe too large, we will give you Q queries, each one contains a number m, you need to tell me the m-th number in the death sequence.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=3e6+10; const int maxn=1e4+220; const double eps=1e-12; int n,k,q,ans[N],sum[N],high; pair<int,int>p[N]; inline void counter(int x) { if(x%k==0)p[x].first=1,p[x].second=x/k; else p[x].first=p[x-x/k-1].first+1,p[x].second=p[x-x/k-1].second; sum[p[x].first]++; high=max(high,p[x].first); } inline void Init() { high=0; for(int i=0;i<=n;i++)sum[i]=0; for(int i=0;i<n;i++)counter(i); for(int i=1;i<=high;i++)sum[i]+=sum[i-1]; for(int i=0;i<n;i++) { int temp=sum[p[i].first-1]+p[i].second+1; ans[temp]=i+1; } } int main() { int t; read(t); while(t--) { read(n);read(k);read(q); Init(); int x; while(q--) { read(x); print(ans[x]); } } return 0; }