您的位置:首页 > Web前端

UVA 11754 Code Feat (枚举,中国剩余定理)

2014-11-27 22:24 435 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud

C

Code Feat

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: SameeZahur

当所有k的乘积较小时,直接枚举出所有的情况,然后用中国剩余定理(CRT)即可求解;

当所有k的乘积较大时,直接枚举所有值,判断是否符合即可。注意k/x越小越好,这样t*x+yi足够大,很快就能找到。

#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
long long X[11],K[11],Y[11][110];
vector<long long >vec;
set<int>val[11];
long long c,s;
long long mod;
int minn=0;
typedef long long ll;
void ext_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b){d=a;x=1;y=0;}
else
{
ext_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll a[11];
ll CRT()
{
ll d,x,y,ret=0;
ll temp;
for(int i=0;i<c;i++)
{
temp=mod/X[i];
ext_gcd(X[i],temp,d,x,y);
ret=(ret+y*temp*a[i])%mod;
}
return (ret+mod)%mod;
}
void dfs(int d)
{
if(d==c)vec.push_back(CRT());
else
{
for(int i=0;i<K[d];i++)
{
a[d]=Y[d][i];
dfs(d+1);
}
}
}
void solve1()
{
vec.clear();
dfs(0);
sort(vec.begin(),vec.end());
int size=vec.size();
int num=0;
for(int i=0;;i++)
{
for(int j=0;j<size;j++)
{
ll ans=mod*i+vec[j];
if(ans>0)
{
printf("%lld\n",ans);
num++;
if(num==s)return ;
}
}
}
}
void solve2()
{
for(int i=0;i<c;i++)
{
if(i!=minn)
{
val[i].clear();
for(int j=0;j<K[i];j++)
{
val[i].insert(Y[i][j]);
}
}
}
ll ans=0;
bool ok=1;
int num=0;
for(int i=0;;i++)
{
for(int j=0;j<K[minn];j++)
{
ans=X[minn]*i+Y[minn][j];
if(ans<=0)continue;
ok =1;
for(int k=0;k<c;k++)
{
if(k!=minn&&!val[k].count(ans%X[k]))
{
ok=0;
break;
}
}
if(ok)
{
printf("%lld\n",ans);
num++;
if(num==s)return;
}
}
}
}
int main()
{
while(scanf("%lld%lld",&c,&s)==2&&(c||s))
{
if(c==0&&s==0)break;
mod=1;
minn=0;
long long k=1;
for(int i=0;i<c;i++)
{
scanf("%lld%lld",&X[i],&K[i]);
mod*=X[i];
k*=K[i];
for(int j=0;j<K[i];j++)
{
scanf("%lld",&Y[i][j]);
}
sort(Y[i],Y[i]+K[i]);
if(K[i]*X[minn]>K[minn]*X[i])minn=i;
}
if(k>10000)solve2();
else solve1();
printf("\n");

}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: