Topic link: https://nanti.jisuanke.com/t/41352
The meaning of the title is easy to understand. Not many people have seen it. They feel frightened by the amount of passage. In fact, it's a water problem. Just reverse simulation.
Using queue simulation, reverse simulation, it needs to put m cards in the back, then I put m cards in the front. At first, there was only one card in the queue, and slowly added to n cards.
First increase the card, then all the way to 1 card. By the way, there may be only five cards, but m is 6, 7, 8 or 9, 10. So in order to reduce unnecessary simulation,
Use mod to reduce, because some simulation will make the card compare with before, it will remain intact.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <map> 9 #include <cmath> 10 #include <iomanip> 11 using namespace std; 12 13 typedef long long LL; 14 #define inf 1e9 15 #define rep(i,j,k) for(int i = (j); i <= (k); ++i) 16 #define rep__(i,j,k) for(int i = (j); i < (k); ++i) 17 #define per(i,j,k) for(int i = (j); i >= (k); --i) 18 #define per__(i,j,k) for(int i = (j); i > (k); --i) 19 20 const int N = 40000010; 21 int arr[N]; 22 int mod,n,q; 23 queue<int> que; 24 25 void work(){ 26 27 int tmp; 28 que.push(n);//Press in the largest card 29 int num = 1; 30 int end = n - 1;//The last 1 card is taken away directly, so there is no need to simulate that round. 31 while(num <= end){ 32 tmp = mod % num; 33 34 rep(o,1,tmp){ 35 que.push(que.front()); 36 que.pop(); 37 } 38 que.push(n-num);//Press in the previous card 39 ++num; 40 } 41 // que.push(1); 42 } 43 44 int main(){ 45 46 int T; 47 scanf("%d",&T); 48 49 while(T--){ 50 scanf("%d%d",&n,&mod); 51 52 rep(i,1,n){ 53 arr[i] = i;//1-n Card 54 } 55 scanf("%d",&q); 56 57 work(); 58 59 per(i,n,1){//Remove card 60 arr[i] = que.front(); 61 que.pop(); 62 } 63 64 int o; 65 rep(i,1,q){ 66 scanf("%d",&o); 67 printf("%d\n",arr[o]); 68 } 69 70 // rep(i,1,n) printf("%d ",arr[i]); 71 } 72 73 getchar(); getchar(); 74 return 0; 75 }