Sicily 7763. Another lottery 解题报告
题目:
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Even in times of an economic crisis, people in Byteland still like to participate in lotteries. With a bit of luck, they might get rid of all their sorrows and become rich.
The most popular lottery in Byteland consists of m rounds. In each round, everyone can purchase as many tickets as he wishes, and among all tickets sold in this round, one ticket is chosen randomly, each one with the same probability. The owner of that ticket wins the prize money of this round. Since people in Byteland like powers of 2, the prize money for the winner of round i amounts to 2i Bytelandian Dollars.
Can you determine for each participant in the lottery the probability that he will win more money than anybody else?
Input
The input consists of several test cases. Each test case starts with a line containing two integers n and m, the number of participants in the lottery and the number of rounds in the lottery. You may assume that 1 ≤ n ≤ 10000 and 1 ≤ m ≤ 30.
The following n lines contain the description of the tickets bought by the participant. The ith such line contains m non-negative integers c1, ..., cm, where cj (1 ≤ j ≤ m) is the amount of tickets of round j bought by partipant i. The total number of tickets sold in each round is between 1 and 109.
The input ends with a line containing 2 zeros.
Output
For each test case, print n lines of output, where line i contains the probability as a reduced fraction that participant i wins the most money. See the sample output for details.
Sample Input
5 4 3 1 2 3 3 1 2 4 3 1 3 5 4 4 4 0 5 5 0 0 1 1 1 0 0
Sample Output
1 / 4 1 / 3 5 / 12 0 / 1 0 / 1 1 / 1
分析:
由于第i轮彩票的金额是2的i次方,因此只要赢得最后一轮彩票的人肯定得到最多的钱(即使另一个人赢了前面所有轮次)。因此只需要将最后一轮每个人买了多少张存进数组,算出总张数,即可算出每个人赢得最后一轮的概率。由于输出要用分数格式,所以开了个Fraction类来处理分数。一开始用cin老是超时,无奈改成scanf通过(感觉违背了C++的原则额...写出的程序不论不类的...),不过凡事要循环多次进行输入输出而且时间限制的题目可以考虑用scanf,printf效率较高
1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 class Fraction{ 6 private: 7 int numerator; //分子 8 int denominator; //分母 9 public: 10 void getData(int n,int d){ 11 numerator=n; 12 denominator=d; 13 } 14 int gcd(int x,int y){ //最大公约数 15 int temp; 16 if(x<y){ 17 temp=x,x=y,y=temp; 18 } 19 while(y!=0){ 20 temp=x%y,x=y,y=temp; 21 } 22 return x; 23 } 24 void simplify(){ //化简分数 25 if(numerator==0) 26 denominator=1; 27 else{ 28 int temp=gcd(numerator,denominator); 29 numerator/=temp; 30 denominator/=temp; 31 } 32 } 33 void display(){ 34 simplify();//输出之前先化简分数 35 printf("%d / %d\n",numerator,denominator); 36 } 37 }; 38 39 int main(){ 40 int numOfParticipants,numOfRounds; 41 while(scanf("%d %d",&numOfParticipants,&numOfRounds) 42 &&!(numOfParticipants==0&&numOfRounds==0)){ 43 int lastRound[numOfParticipants]; //只有最后一轮数据有用,存进一个数组中 44 int noUsed; 45 int sum=0; 46 for(int i=0;i<numOfParticipants;i++){ 47 for(int j=0;j<numOfRounds-1;j++) //读取前面没用的数据 48 scanf("%d",&noUsed); 49 scanf("%d",&lastRound[i]); 50 sum+=lastRound[i]; 51 } 52 Fraction result[numOfParticipants]; 53 for(int i=0;i<numOfParticipants;i++){ 54 result[i].getData(lastRound[i],sum); 55 result[i].display(); 56 } 57 } 58 return 0; 59 }