UVa 11754 - Code Feat (中国剩余定理 枚举)

C

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 Xs in each test case are pairwise relatively prime (ie, they have no common factor except 1).  Also, the product of the Xs 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                              

Sample Output

3 2

2 1 1

5 2 0 3

3 2 1 2

0 0

5

13

 

Problem Setter: Derek Kisman, Special Thanks: Samee Zahur

 



题意:

有一个正整数N满足C个条件,每个条件都形如“它除以X的余数在集合{Y1,Y2...Yk}中”,所有条件中的X两两互素,你的任务是找出最小的S个解。按照从小到大排序。


思路:

终于写了一次中国剩余定理

入大白上所说,如果所有K的乘积不大,那么可以直接枚举每一个条件里面选择的数,然后用中国剩余定理求出解。

但是如果所有K的乘积过大,则这个方法就太慢了,考虑选择一个K/X最小的条件,然后枚举所有符合这个条件的数n,即 tX+Yi ,t=0,1,2.... 然后依次判断这个n是否符合其它所有条件。



附上两枚感觉很好的链接

中国剩余定理

扩展欧几里德算法 线性同余方程 中国剩余定理



#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i) sol;

void extendedGcd(LL a, LL b, LL & d, LL & x, LL & y) {
    if(!b) { d = a; x = 1; y = 0; }
    else { extendedGcd(b, a%b, d, y, x); y -= x*(a/b); }
}

LL china(int n, int * a, int * m) {
    LL M = 1, d, y, x = 0;
    for(int i=0; i 0) { P64I(n); if(--S == 0) break; }
        }
    }
}

set values[maxc];
void solveEnum(int bc) {
    for(int i=0; i




你可能感兴趣的:(数学,ACM,枚举)