1054 - Pilgrimage

Jack is making a long distance walk with some friends along the old pilgrim road from Vézelay to Santiago de Compostela. Jack administers money for the group. His administration is quite simple. Whenever an amount (€ 60, say) has to be paid for the common good he will pay it, and write in his booklet: PAY 60.

When needed, Jack will ask every member of the group, including himself, to pay an amount (€ 50, say) to the collective purse, and write in his booklet: COLLECT 50. If the group size is 7, he collects € 350 in total.

Unfortunately some of the group members cannot participate in the full walk. So sometimes the group will grow, sometimes it will shrink. How does Jack handle these comings and goings of group members in terms of collective money? Suppose, for example, the group size is 7, and that Jack has € 140 in cash, which is € 20 for every group member. If two group members leave, each will receive € 20, and Jack will write in his booklet: OUT 2. If under the same circumstances three new group members arrive, they will each have to pay € 20, and Jack will write: IN 3.

In these cases the amount in cash could easily be divided, without fractions. As a strange coincidence, this happened during the whole trip. Jack never had to make calculations with fractional numbers of euros.

Near the end of the trip, Jack was joined by all his fellow travelers. Nobody was willing to miss the glorious finale of the trip. It was then that Jack tried to remember what the group size had been during each part of the trip. He could not remember.

Given a page of Jack's booklet, could you figure out the size of the group at the beginning of that page?

Input 

The input file contains several test cases. Each test case is a sequence of lines in Jack's booklet. The first line of each test case will give the number N (0 < N$ \le$50) of lines to follow. The next N lines have the format: keyword > num >, where


keyword > = PAY | COLLECT | IN | OUT


and num > is a positive integer, with the following restrictions:


IN kk$ \le$20
OUT kk$ \le$20
COLLECT kk$ \le$200
PAY kk$ \le$2000


The last case is followed by a line containing a single zero.

Output 

For each test case, print a single line describing the size of the group at the beginning of the part of the trip described in the test case. This line contains:

  • The word `IMPOSSIBLE', if the data are inconsistent.
  • A single number giving the size of the group just prior to the sequence of lines in Jack's booklet, if this size is uniquely determined by the data.
  • Several numbers, in increasing order, separated by spaces, giving the possible sizes of the group, in case the number of solutions is finite, but the solution is not unique.
  • A statement in the format: `SIZE >= N', giving a lower bound for the size of the group, in case the number of solutions is infinite. Observe that the inequality SIZE >= 1 always applies, since at least Jack himself did the whole trip.

Sample Input 

5 
IN 1 
PAY 7 
IN 1 
PAY 7 
IN 1 
7 
IN 1 
COLLECT 20 
PAY 30 
PAY 12 
IN 2 
PAY 30 
OUT 3 
3 
IN 1 
PAY 8 
OUT 3 
1 
OUT 5 
0

Sample Output 

IMPOSSIBLE 
2 
3 7 
SIZE >= 6

Claimer: The data used in this problem is unofficial data prepared by Derek Kisman. So any mistake here does not imply mistake in the offcial judge data. Only Derek Kisman is responsible for the mistakes. Report mistakes to dkisman@acm.org







#include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MaxP=200*50;
char s[20];
int n,m,i,j,k,P,sum,delta,min;
int list[50][2];
int a[MaxP+1];

int main()
{
	while(scanf("%d",&n)&&n)
	{
		sum=0;P=0;
		for(i=0;i<n;i++)
		{
			scanf("%s %d",s,&m);
			if(s[0]=='C')
				continue;
			if(s[0]=='P')
				P+=m;
			else
			{
				if(P&&sum)
				{
					list[sum][0]=2;
					list[sum][1]=P;
					sum++;
				}
				if(s[0]=='I')
					list[sum][0]=0;
				else
					list[sum][0]=1;
				list[sum][1]=m;
				sum++;
				P=0;
			}
		}
		memset(a,0,sizeof(a));
		delta=0;min=0;P=0;
		for(i=0;i<sum;i++)
		{
			if(list[i][0]==0)
				delta+=list[i][1];
			if(list[i][0]==1)
				delta-=list[i][1];
			if(min>delta)
				min=delta;
			if(list[i][0]!=2)
				continue;
			P++;
			for(j=1;j<=list[i][1];j++)
				if(j>delta&&list[i][1]%j==0)
					a[j-delta]++;
		}
		if(P==0)
			printf("SIZE >= %d\n",1-min);
		else
		{
			k=0;
			for(i=1-min;i<=MaxP;i++)
				if(a[i]==P)
				{
					if(k)
						printf(" ");
					k=i;
					printf("%d",k);
				}
			if(!k)
				printf("IMPOSSIBLE");
			printf("\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值