Another lottery

来源:互联网 发布:知乎怎么做微信销售 编辑:程序博客网 时间:2024/05/01 08:10

题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=3905

题目描述:

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?

输入格式:

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.

输出格式:

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.

输入样例:

5 43 1 2 33 1 2 43 1 3 54 4 4 05 5 0 01 110 0
输出样例:

1 / 41 / 35 / 120 / 10 / 11 / 1
分析思路,不说了,水题,就是fenzi和g数组开小了,但是为什么数组开小也会WA。。。至今仍不明白~~~不说了,反正AC了

源代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXN 35#define MAXM 10010long long nn, mm;long long c[MAXM][MAXN];long long fenzi[MAXM], g[MAXM];long long gcd(long long aa, long long bb) {    return bb == 0 ? aa : gcd(bb, aa % bb);}int main() {    //FILE *p = freopen("test.txt", "r", stdin);    long long i, j, k;    while (scanf("%d%d", &nn, &mm) != EOF && (nn || mm)) {        for (i = 0; i < nn; i++) {            for (j = 0; j < mm; j++) {                scanf("%d", &c[i][j]);            }        }        long long sum;        memset(fenzi, 0, sizeof(fenzi));        memset(g, 0, sizeof(g));        int flag = 0;        for (i = mm - 1; i >= 0; i--) {            sum = 0;            for (j = 0; j < nn; j++) {                sum += c[j][i];            }            if (sum > 0) {                for (k = 0; k < nn; k++) {                    fenzi[k] = c[k][i];                }                flag = 1;                break;            }        }        if (!flag)            for (i = 0; i < nn; i++) {                printf("0 / 1\n");            }        else {            for (i = 0; i < nn; i++) {                g[i] = gcd(sum, fenzi[i]);            }            for (i = 0; i < nn; i++) {                printf("%lld / %lld\n", fenzi[i] / g[i], sum / g[i]);            }        }    }    return 0;}
运行结果:



0 0
原创粉丝点击