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 <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

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)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxc = 20;
const int maxs = 20;
const int maxk = 100 + 20;
int C, S;
int X[maxc], K[maxc];
int G[maxc][maxk];
int ans[maxs];
int sel[maxc];
vector<LL> 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<n; i++) M *= m[i];
    for(int i=0; i<n; i++) {
        LL w = M / m[i];
        extendedGcd(m[i], w, d, d, y);
        x = (x + y*w*a[i]) % M;
    }
    return (x+M) % M;
}

void dfs(int cur) {
    if(cur == C) {
        LL tans = china(C, sel, X);
        sol.push_back(tans);
        return ;
    }
    for(int i=0; i<K[cur]; i++) {
        sel[cur] = G[cur][i];
        dfs(cur+1);
    }
}

void solveChina() {
    sol.clear();
    dfs(0);
    sort(sol.begin(), sol.end());
    LL M = 1;
    for(int i=0; i<C; i++) M *= X[i];
    for(int i=0; S; i++) {
        for(int j=0; j<sol.size(); j++) {
            LL n = M * i + sol[j];
            if(n > 0) { P64I(n); if(--S == 0) break; }
        }
    }
}

set<int> values[maxc];
void solveEnum(int bc) {
    for(int i=0; i<C; i++) if(i != bc) {
        values[i].clear();
        for(int j=0; j<K[i]; j++) values[i].insert(G[i][j]);
    }
    for(int t = 0; S; t++) {
        for(int i=0; i<K[bc]; i++) {
            LL x = (LL)t * X[bc] + G[bc][i];
            if(!x) continue;
            bool ok = true;
            for(int c=0; c<C; c++) if(c != bc) {
                if(!values[c].count(x%X[c])) { ok = false; break; }
            }
            if(ok) { P64I(x); if(--S == 0) break; }
        }
    }
}

int main() {

    while(scanf("%d%d", &C, &S) != EOF && C) {
        int totK = 1;
        int tmini = 0;
        for(int i=0; i<C; i++) {
            scanf("%d%d", &X[i], &K[i]);
            totK *= K[i];
            if(K[i] * X[tmini] < X[i] * K[tmini]) tmini = i;
            for(int j=0; j<K[i]; j++) {
                scanf("%d", &G[i][j]);
            }
            sort(G[i], G[i]+K[i]);
        }
        if(totK <= 10000) solveChina();
        else solveEnum(tmini);
        putchar('\n');
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值