uva11754 - Code Feat 枚举 中国剩余定理

来源:互联网 发布:电气控制仿真软件exe 编辑:程序博客网 时间:2024/05/02 02:52

Hooray!  Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed the moles in the government,prevented an environmental catastrophe, and found homes for three orphanedkittens, all in the span of 19 consecutive hours.  But now, he only has 5 hours remaining todeal with his final challenge: an activated nuclear bomb protected by asecurity code.  Can you help him figureout the code and deactivate it?  Eventsoccur in real time.

The governmenthackers at CTU (Counter-Terrorist Unit) have learned some things about thecode, 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 "whendivided by X, the remainder is one of {Y1, Y2, Y3, ...,Yk}".There are multiple solutions to these clues, but the code is likely tobe one of the smallest ones.  So they'dlike you to print out the first few solutions, in increasing order.

The world iscounting on you!

Input

Input consistsof several test cases.  Each test casestarts with a line containing C, the number of clues (1 <= C <= 9), andS, the number of desired solutions (1 <= S <= 10).  The next C lines each start with two integersX (2 <= X) and k (1 <= k <= 100), followed by the k distinct integersY1, Y2, ..., Yk (0 <= Y1,Y2, ..., Yk < X).

You may assumethat the Xs in each test case are pairwise relativelyprime (ie, they have no common factor except 1).  Also, the product of the Xs will fit into a32-bit integer.

The last testcase is followed by a line containing two zeros.

Output

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

Print a blankline 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

  从小到大输出S个数,这个数满足对每个Xi取余,余数都在Y[i]中。

  k[i]的积比较小的时候用DFS枚举每个Y的取值,用中国剩余定理求解。k[i]的积比较大的时候书上说选一个k/X最小的i,枚举Xi*t+Y[i][j],再看这个数对于其他的满不满足。

  不是很明白为什么大小数据都用第二种枚举方法会超时。。

#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>#define eps 1e-9#define MAXN 15#define MAXM 110#define MOD 1000000007typedef long long LL;using namespace std;const int LIMIT=10000;int C,S,X[MAXN],k[MAXN],Y[MAXN][MAXM],a[MAXN];set<int> values[MAXN];vector<LL> sol;void solve_enum(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(Y[i][j]);    }    for(int i=0;S;i++)        for(int j=0;j<k[bc];j++){            LL n=(LL)X[bc]*i+Y[bc][j];            if(!n) continue;            int flag=1;            for(int c=0;c<C;c++) if(c!=bc&&!values[c].count(n%X[c])){                flag=0;                break;            }            if(flag){                printf("%lld\n",n);                if(!--S) break;            }        }}void gcd(LL a,LL b,LL& d,LL& x,LL& y){    if(!b){        d=a;        x=1;        y=0;        return;    }    gcd(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];        gcd(m[i],w,d,d,y);        x=(x+y*w*a[i])%M;    }    return (x+M)%M;}void DFS(int dep){    if(dep==C) sol.push_back(China(C,a,X));    else for(int i=0;i<k[dep];i++){        a[dep]=Y[dep][i];        DFS(dep+1);    }}void solve_China(){    sol.clear();    DFS(0);    sort(sol.begin(),sol.end());    LL M=1;    int L=sol.size();    for(int i=0;i<C;i++) M*=X[i];    for(int i=0;S;i++)        for(int j=0;j<L;j++){            LL n=M*i+sol[j];            if(n>0){                printf("%lld\n",n);                if(!--S) break;            }        }}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d%d",&C,&S)!=EOF&&(C||S)){        int bestc=0;        LL tot=1;        for(int i=0;i<C;i++){            scanf("%d%d",&X[i],&k[i]);            tot*=k[i];            for(int j=0;j<k[i];j++) scanf("%d",&Y[i][j]);            sort(Y[i],Y[i]+k[i]);            if(k[i]*X[bestc]<k[bestc]*X[i]) bestc=i;        }        if(tot>LIMIT) solve_enum(bestc);        else solve_China();        printf("\n");    }    return 0;}


0 0
原创粉丝点击