UVA

来源:互联网 发布:大学生网络安全意识 编辑:程序博客网 时间:2024/06/05 01:14

Code Feat

Hooray! Agent Bauer has shot the terrorists, blown up the bad guy base, saved the hostages, exposed the moles in the government, prevented an environmental catastrophe, and found homes for three orphaned kittens, all in the span of 19 consecutive hours. But now, he only has 5 hours remaining to deal with his final challenge: an activated nuclear bomb protected by a security code. Can you help him figure out the code and deactivate it? Events occur in real time.The government hackers at CTU (Counter-Terrorist Unit) have learned some things about the code,but they still haven’t quite solved it. They know it’s a single, strictly positive, integer. They also know several clues of the form “when divided by X, the remainder is one of {Y1, Y2, Y3, . . . , Yk}”. There are multiple solutions to these clues, but the code is likely to be one of the smallest ones. So they’d like you to print out the first few solutions, in increasing order.The world is counting on you!

Input

 Input consists of several test cases. Each test case starts with a line containing C, the number of clues(1 ≤ C ≤ 9), and S, the number of desired solutions (1 ≤ S ≤ 10). The next C lines each start with two integers X (2 ≤ X) and k (1 ≤ k ≤ 100), followed by the k distinct integers Y1, Y2, . . . , Yk(0 ≤ Y1, Y2, . . . , Yk < X).You may assume that the X’s in each test case are pairwise relatively prime (ie, they have no common factor except 1). Also, the product of the X’s will fit into a 32-bit integer.The last test case is followed by a line containing two zeros.

Output

For each test case, output S lines containing the S smallest positive solutions to the clues, in increasing order.

Print a blank line after the output for each test case.

Sample Input

3 2

2 1 1

5 2 0 3

3 2 1 2

0 0

Sample Output

5

13


题目大意:有一个正整数N满足C个条件,每个条件都形如“它除以X的余数在集合{Y1,Y2,...,Yk}中”,所有条件中的X两两互素,你的任务是找出满足条件的最小的S个N;

题目思路:本题是《算法竞赛入门经典-训练指南》里的一道例题,是具有一定思考量的一道中国剩余定理的例题。解决本题的方法要分两类来讨论,当所有X的k的乘积较小时我们可以直接暴力枚举每一个X的余数Yx,然后全部都用中国剩余定理来求解,最后求出前S小的解;但如果k的乘积较大的时候直接暴力枚举是肯定会超时的,按题目所给条件最多的枚举情况为100^9;所以当k的乘积过大时,我们可以直接枚举X,找到k/X最小的那个X来进行枚举就可以大大地减少时间复杂度,具体实现看代码。


AC代码如下:

#include <bits/stdc++.h>using namespace std;typedef long long LL;typedef pair<int, int>pii;const int MX = 100 + 10;int C, X[15], k[15];int Y[15][MX], a[15];set<int>val[MX];vector<LL>sol;void solve_enum(int S, int bc) {    for(int c = 0; c < C; c++)        if(c != bc) {            val[c].clear();            for(int i = 0; i < k[c]; i++)                val[c].insert(Y[c][i]);//利用STL里的set进行查找可以加快查找速度;        }    for(int t = 0; S != 0; t++) {        for(int i = 0; i < k[bc]; i++) {            LL n = (LL)X[bc] * t + Y[bc][i];//枚举满足选出的X的解n;            if(n == 0) continue;//由于题目要求正整数解,所以n=0时跳过;            int ok = 1;            for(int c = 0; c < C; c++) {                if(c != bc) {                    if(!val[c].count(n % X[c])) {//对枚举出的n进行验证,看n是否满足于其他C-1个X;                        ok = 0;                        break;                    }                }            }            if(ok) {                printf("%lld\n", n);                if(--S == 0)                    break;            }        }    }}LL ex_gcd(LL a, LL b, LL &d, LL &x, LL &y) {    if(!b) {        d = a;        x = 1;        y = 0;    } else {        ex_gcd(b, a % b, d, y, x);        y -= x * (a / b);    }}LL crt() {//普通的中国剩余定理模板,来对暴力枚举的方法进行求解;    LL M = 1, ans = 0;    for(int i = 0; i < C; i++)        M *= X[i];    for(int i = 0; i < C; i++) {        LL m = M / X[i];        LL x1, x2, d;        ex_gcd(m, X[i], d, x1, x2);        ans = (ans + m * x1 * a[i]) % M;    }    return (ans + M) % M;}void dfs(int dep) {//用dfs枚举出所有可能的情况,将答案存储于vector之中;    if(dep == C) {        sol.push_back(crt());        return;    }    for(int i = 0; i < k[dep]; i++) {        a[dep] = Y[dep][i];        dfs(dep + 1);    }}void solve_crt(int S) {    sol.clear();    dfs(0);    sort(sol.begin(), sol.end());//暴力枚举求解之后排序,求得前S小的解;    LL M = 1;    for(int i = 0; i < C; i++) M *= X[i];    for(int i = 0; S != 0; i++) {        for(int j = 0; j < sol.size(); j++) {            LL n = M * i + sol[j];//由于由中国剩余定理求得的解可能少于S个,所以要加上k个M使其满足S个,由于M是所有X的lcm,所以加多少对最后取膜不影响;            if(n > 0) {                printf("%lld\n", n);                if(--S == 0)                    break;            }        }    }}int main() {    int S;    while(~scanf("%d%d", &C, &S)) {        if(C == 0 && S == 0) break;        LL tot = 1;        int bestc = 0;        for(int c = 0; c < C; c++) {            scanf("%d%d", &X[c], &k[c]);            tot *= k[c];            for(int i = 0; i < k[c]; i++) scanf("%d", &Y[c][i]);            sort(Y[c], Y[c] + k[c]);//记得将Y排个序,在k太大的情况时枚举X可以保证得到的结果是从小到大的;            if(k[c]*X[bestc] < k[bestc]*X[c]) bestc = c;//找出k/X最小的那一位;注:k[c]/X[c] < k[bestc]/X[bestc] -> k[c]*X[bestc] < k[bestc]*X[c];        }        if(tot > 10000)//对k的乘积进行判断,分类来解决问题;            solve_enum(S, bestc);        else            solve_crt(S);        printf("\n");    }    return 0;}