Card Stacking
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3927 Accepted: 1541 Description
Bessie is playing a card game with her N-1 (2 ≤ N ≤ 100) cow friends using a deck with K (N ≤ K ≤ 100,000; 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 P (1 ≤ P ≤ 10) 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.
Input
* Line 1: Three space-separated integers: N, K, and P
Output
* 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.
Sample Input
3 9 2Sample Output
3
7
8
题意:
Bessie跟朋友玩牌,总共n人k张牌,k是n的整数倍。k张牌*有k/n张好牌,Bessie想要作弊得到所有好牌。可Bessie的同伴为了防止Bessie作弊而设定了如下规则:
1.每次发牌从牌顶发,从Bessie右手边第一个人开始。
2.每发出一张牌就要将接下来的p张牌依次放至牌底。
按照这个方法发完所有牌。即使是这样,Bessie依然想要拿到所有好牌,请你帮她计算出将好牌放在哪些位置可以实现Bessie的愿望。
用队列可以很方便的解决这个问题,将所有k张牌入队,发牌即为出队,过牌即出队后再入队,每循环到Bessie时将队首元素记录下来,即为好牌位置。
附AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
using namespace std; const int MAX=; int ans[MAX];
int main(){
int n,k,p;
queue<int> q;//定义队列
memset(ans,,sizeof(ans));
while(~scanf("%d %d %d",&n,&k,&p)){
for(int i=;i<=k;i++){//入队
q.push(i);
}
int temp=,sum=;
while(){
if(temp++%n==){//temp从1开始每循环至Bessie处则为好牌
ans[sum++]=q.front();
if(sum==k/n){//好牌放完则退出循环
break;
}
}
q.pop();//发出的牌出队
for(int i=;i<p;i++){//其他牌移到牌底
q.push(q.front());
q.pop();
}
}
sort(ans,ans+sum);
for(int i=;i<sum;i++){
printf("%d\n",ans[i]);
}
}
return ;
}