题目描述 

Bessie is playing a card game with her N-1 cow friends using a deck with K ; K is a multiple of N) cards. The deck contains M = K/N "good" cards and K-M "bad" cards. Bessie is the dealer and, naturally, wants to deal herself all of the "good" cards. She loves winning. Her friends suspect that she will cheat, though, so they devise a dealing system in an attempt to prevent Bessie from cheating. They tell her to deal as follows: 1. Start by dealing the card on the top of the deck to the cow to her right 2. Every time she deals a card, she must place the next cards on the bottom of the deck; and 3. Continue dealing in this manner to each player sequentially in a counterclockwise manner Bessie, desperate to win, asks you to help her figure out where she should put the "good" cards so that she gets all of them. Notationally, the top card is card #1, next card is #2, and so on.

输入描述:

* Line 1: Three space-separated integers: N, K, and P

输出描述:

* Lines 1..M: Positions from top in ascending order in which Bessie should place "good" cards, such that when dealt, Bessie will obtain all good cards.

示例1

输入
3 9 2
输出
3
7
8
说明
Bessie should put the "good" cards in positions 3, 7, and 8. The cards will be dealt as follows; the card numbers are "position in original deck":
Card Deck         P1      P2    Bessie
Initial configuration           1 2 3 4 5 6 7 8 9  - - -   - - -   - - -
Deal top card [1] to Player 1   2 3 4 5 6 7 8 9    1 - -   - - -   - - -
Top card to bottom (#1 of 2)    3 4 5 6 7 8 9 2    1 - -   - - -   - - -
Top card to bottom (#2 of 2)    4 5 6 7 8 9 2 3    1 - -   - - -   - - -
Deal top card [4] to Player 2   5 6 7 8 9 2 3      1 - -   4 - -   - - -
Top card to bottom (#1 of 2)    6 7 8 9 2 3 5      1 - -   4 - -   - - -
Top card to bottom (#2 of 2)    7 8 9 2 3 5 6      1 - -   4 - -   - - -
Deal top card [7] to Bessie     8 9 2 3 5 6        1 - -   4 - -   7 - -
Top card to bottom (#1 of 2)    9 2 3 5 6 8        1 - -   4 - -   7 - -
Top card to bottom (#2 of 2)    2 3 5 6 8 9        1 - -   4 - -   7 - -
Deal top card [2] to Player 1   3 5 6 8 9          1 2 -   4 - -   7 - -
Top card to bottom (#1 of 2)    5 6 8 9 3          1 2 -   4 - -   7 - -
Top card to bottom (#2 of 2)    6 8 9 3 5          1 2 -   4 - -   7 - -
Deal top card [6] to Player 2   8 9 3 5            1 2 -   4 6 -   7 - -
Top card to bottom (#1 of 2)    9 3 5 8            1 2 -   4 6 -   7 - -
Top card to bottom (#2 of 2)    3 5 8 9            1 2 -   4 6 -   7 - -
Deal top card [3] to Bessie     5 8 9              1 2 -   4 6 -   7 3 -
Top card to bottom (#1 of 2)    8 9 5              1 2 -   4 6 -   7 3 -
Top card to bottom (#2 of 2)    9 5 8              1 2 -   4 6 -   7 3 -
Deal top card [9] to Player 1   5 8                1 2 9   4 6 -   7 3 -
Top card to bottom (#1 of 2)    8 5                1 2 9   4 6 -   7 3 -
Top card to bottom (#2 of 2)    5 8                1 2 9   4 6 -   7 3 -
Deal top card [5] to Player 2   8                  1 2 9   4 6 5   7 3 -
Top card to bottom (#1 of 2)    8                  1 2 9   4 6 5   7 3 -
Top card to bottom (#1 of 2)    8                  1 2 9   4 6 5   7 3 -
Deal top card [8] to Bessie     -                  1 2 9   4 6 5   7 3 8
Bessie will end up with the "good cards" that have been placed in positions 3, 7, and 8 in the original deck.

解答

这题的意思就是一个叫Bessie的人和他n-1个朋友玩发牌游戏,一共有k张牌,有m=k/n张好牌(k/m张坏牌),从右开始发,也就是说Bessie是最后一个拿到牌的,每发一张牌都需要将最上面连续的p张牌放到下面,Bessie想要获胜,所以请你找出应该把好牌放在那些位置(就是找出Bessie获得牌的位置)并升序输出,在名义上,顶部卡是#1卡,下一张卡是#2,依此类推。
解题思路:
用一个queue数组模仿队列,一个头下标,一个尾下标,然后每发1张牌就将p张牌放到数组最后面,循环发出n-1张牌,下一张要发的牌就是好牌,一共要发k/n张好牌,下面是AC代码:
#include"iostream"
#include"algorithm"

using namespace std;

const int MAX = 10000007; 
int queue[MAX];//模仿队列数组 
int n,k,p;//人数,牌数,需要放到下面的牌数 
int good_cards[MAX];//存放好牌 
int size;//好牌数组的下标 

int main()
{
	cin >> n >> k >> p;
	for(int i=1;i<=k;i++)//初始化队列
		queue[i] = i;
	int top=1;//队列的头 
	int bot=k+1;//队列的尾 
	for(int q=1;q<=k/n;q++)//一共要发出k/n张好牌 
	{
		for(int j=0;j<n-1;j++)//从右边开始发牌,发n-1次 
		{	
			top++;
			for(int i=0;i<p;i++)//将p张牌依次放到最后 
				queue[bot++]=queue[top++];
		}
		good_cards[size++] = queue[top++];//存储好牌 
		for(int i=0;i<p;i++)//将p张牌依次放到最后
			queue[bot++]=queue[top++];	
	}
	sort(good_cards,good_cards+size);//升序 
	for(int i=0;i<size;i++)
		cout << good_cards[i] << endl;
	return 0;
}

来源:Friends233