转载请注明出处: 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[],K[],Y[][];
vector<long long >vec;
set<int>val[];
long long c,s;
long long mod;
int minn=;
typedef long long ll;
void ext_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b){d=a;x=;y=;}
else
{
ext_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll a[];
ll CRT()
{
ll d,x,y,ret=;
ll temp;
for(int i=;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=;i<K[d];i++)
{
a[d]=Y[d][i];
dfs(d+);
}
}
}
void solve1()
{
vec.clear();
dfs();
sort(vec.begin(),vec.end());
int size=vec.size();
int num=;
for(int i=;;i++)
{
for(int j=;j<size;j++)
{
ll ans=mod*i+vec[j];
if(ans>)
{
printf("%lld\n",ans);
num++;
if(num==s)return ;
}
}
}
}
void solve2()
{
for(int i=;i<c;i++)
{
if(i!=minn)
{
val[i].clear();
for(int j=;j<K[i];j++)
{
val[i].insert(Y[i][j]);
}
}
}
ll ans=;
bool ok=;
int num=;
for(int i=;;i++)
{
for(int j=;j<K[minn];j++)
{
ans=X[minn]*i+Y[minn][j];
if(ans<=)continue;
ok =;
for(int k=;k<c;k++)
{
if(k!=minn&&!val[k].count(ans%X[k]))
{
ok=;
break;
}
}
if(ok)
{
printf("%lld\n",ans);
num++;
if(num==s)return;
}
}
}
}
int main()
{
while(scanf("%lld%lld",&c,&s)==&&(c||s))
{
if(c==&&s==)break;
mod=;
minn=;
long long k=;
for(int i=;i<c;i++)
{
scanf("%lld%lld",&X[i],&K[i]);
mod*=X[i];
k*=K[i];
for(int j=;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>)solve2();
else solve1();
printf("\n"); }
return ;
}

UVA 11754 Code Feat (枚举,中国剩余定理)的更多相关文章

  1. uva 11754 Code Feat (中国剩余定理)

    UVA 11754 一道中国剩余定理加上搜索的题目.分两种情况来考虑,当组合总数比较大的时候,就选择枚举的方式,组合总数的时候比较小时就选择搜索然后用中国剩余定理求出得数. 代码如下: #includ ...

  2. UVA - 11754 Code Feat (分块+中国剩余定理)

    对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...

  3. UVA 11754 - Code Feat(数论)

    UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...

  4. UVA 11754 Code Feat 中国剩余定理+枚举

    Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...

  5. Uva 11754 Code Feat

    题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...

  6. UVA 11754 Code Feat 中国剩余定理+暴力

    lrj白书例题,真好 #include <stdio.h> #include <iostream> #include <vector> #include <m ...

  7. Uva 11754(枚举+中国剩余定理)

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

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

    如果直接枚举的话,枚举量为k1 * k2 *...* kc 根据枚举量的不同,有两种解法. 枚举量不是太大的话,比如不超过1e4,可以枚举每个集合中的余数Yi,然后用中国剩余定理求解.解的个数不够S个 ...

  9. UVA 11754 (暴力+中国剩余定理)

    题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...

随机推荐

  1. BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 算法讨论: 1.可以用最大流做,最大流等于最小割. 2.可以把这个图转化其对偶图,然 ...

  2. C语言结构体的字节对齐

    Test Code: #include <iostream> #include <cstring> using namespace std; struct A{ int a; ...

  3. 范围for语句 && 列表初始值&& 标准库函数begin和end

    范围for语句: 引入的意义:简化传统for的编写,主要用于遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for( 声明: 给定序列) { 执行的操作. } 其中,“给定序列 ...

  4. IntelliJ IDEA启动web项目时突然变慢的原因

    在使用IntelliJ IDEA开发web项目过程中,有两次项目启动非常慢,大约要200s的时间: 第一次忘记是怎么解决的,第二次出现后,我就直接重新下载了代码,然后部署,启动,时间有恢复正常,只用了 ...

  5. (转)css中通常会用到浮动与清除,也是一个必须掌握的知识点,概念性的东西不多说,下面举几个例子,来说明它的用法:1.文字环绕效果 2.多个div并排显示 3.清除浮动(默认显示)

    一.文字环绕效果: html代码如下: 1 <body> 2 3 <style type="text/css"> 4 #big img {float: le ...

  6. 使用POI插件,提取导出excel的工具类

    在网站的不同的模块都需要使用到导入导出excel的功能,我们就需要写一个通用的工具类ExcelUtil. 我的思路:首先,导入和导出的Excel的文件格式固定:主标题,二级标题,数据行(姑且就这么叫) ...

  7. shell中的for、while、until

    for var in list do commands done 在每个迭代中,变量var会包含列表中的当前值,第一个迭代会使用列表中的第一个值,第二个迭代使用第二个值. 在do和done中,$var ...

  8. Winform(C#)限制程序只运行一个实例

    C#控制只运行开启一个程序 在这个例子中不需要调用ReleaseMutex,mutex会在程序结束时自动释放.为了防止mutex过早释放,在程序的最后调用下GC.KeepAlive (mutex). ...

  9. 错误:指定的任务可执行文件位置 D:\Android\platform-tools\aapt.exe 无效

    android-apt-compiler: Cannot run program "D:\android-sdk\platform-tools\aapt 装上IntelliJ IDEA /下 ...

  10. awk详解

    一.简介 强大的文本分析工具,基于指定规则浏览和抽取信息.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理.awk有3个不同版本: awk.nawk和ga ...