lrj白书例题,真好

#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
typedef long long LL;
const int N=1e2+;
const int INF=0x3f3f3f3f;
const int LIMIT=;
set<int>values[];
int C,X[],k[];
int Y[][];
void solve_enum(int S,int bc)
{
for(int c=; c<C; ++c)if(c!=bc)
{
values[c].clear();
for(int i=; i<k[c]; ++i)
values[c].insert(Y[c][i]);
}
for(int t=; S!=; ++t)
{
for(int i=; i<k[bc]; ++i)
{
LL n=(LL)X[bc]*t+Y[bc][i];
if(n==)continue;
bool ok=true;
for(int c=; c<C; ++c)if(c!=bc)
if(!values[c].count(n%X[c]))
{
ok=false;
break;
}
if(ok)
{
printf("%lld\n",n);
if(--S==)break;
}
}
}
}
int a[];
vector<LL>sol;
void exgcd(LL a,LL b,LL &d,LL& x,LL& y)
{
if(!b)
{
d=a;
x=;
y=;
}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL china(int n,int* a,int *m)
{
LL M=,d,y,x=;
for(int i=; i<n; ++i)M*=m[i];
for(int i=; i<n; ++i)
{
LL w=M/m[i];
exgcd(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=; i<k[dep]; ++i)
{
a[dep]=Y[dep][i];
dfs(dep+);
}
}
void solve_china(int S)
{
sol.clear();
dfs();
sort(sol.begin(),sol.end());
LL M=;
for(int i=; i<C; ++i)M*=X[i];
for(int i=; S!=; ++i)
{
for(int j=; j<sol.size(); ++j)
{
LL n=M*i+sol[j];
if(n>)
{
printf("%lld\n",n);
if(--S==)break;
}
}
}
}
int main()
{
int S;
while(scanf("%d%d",&C,&S)==&&C)
{
LL tot=;
int bestc=;
for(int c=; c<C; ++c)
{
scanf("%d%d",&X[c],&k[c]);
tot*=k[c];
for(int i=; i<k[c]; ++i)scanf("%d",&Y[c][i]);
sort(Y[c],Y[c]+k[c]);
if(k[c]*X[bestc]<k[bestc]*X[c])bestc=c;
}
if(tot>LIMIT)solve_enum(S,bestc);
else solve_china(S);
printf("\n");
}
return ;
}

UVA 11754 Code Feat 中国剩余定理+暴力的更多相关文章

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

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

  2. UVA 11754 - Code Feat(数论)

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

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at C ...

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

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

  5. Uva 11754 Code Feat

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

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

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

  7. uva11754 中国剩余定理+暴力搜索

    是当y的组合数较小时,暴力枚举所有组合,然后用中国剩余定理求每种组合的解,对解进行排序即可 注意初始解可能是负数,所以如果凑不够S个,就对所有解加上M,2M.... 当y的组合数较大时,选择一个k/x ...

  8. HDU1370Biorhythms(中国剩余定理||暴力)

    Some people believe that there are three cycles in a person's life that start the day he or she is b ...

  9. UVA11754 - Code Feat

    Hooray!  Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed ...

随机推荐

  1. wifidog编译到openwrt

    首先敲一下 cd 命令,定位到自己的用户目录, 然后 mkdir openwrt 新建一个openwrt文件夹,然后开始装openwrt的编译用到的工具, sudo apt-get install g ...

  2. 转 修改oracle用户密码永不过期

      1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的 ...

  3. IT书籍下载汇总--持续更新

    本书单由北北分享,并持续更新,请将该地址加入收藏夹:北北的书单 .badge{float:right;}.list-group-item > .badge + .badge{margin-rig ...

  4. php 删除文件夹及文件

    <?php function deleteDir($dir) { if (!$handle = @opendir($dir)) { return false; } while (false != ...

  5. Yii框架下使用redis做缓存,读写分离

    Yii框架中内置好几个缓存类,其中有memcache的类,但是没有redis缓存类,由于项目中需要做主从架构,所以扩展了一下: /** * FileName:RedisCluster * 配置说明 * ...

  6. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  7. C++ map映射的使用方法

    今天考试做了道题,用上了map,这是一道提高组联赛难度的题目,先发题目: ****************************** 1. A-B problem( dec.c/cpp/pas) . ...

  8. merge into 和 update 的效率对比

    以前只考虑 merge into 只是在特定场合下方便才使用的,今天才发现,merge into 竟然会比 update 在更新数据时有这么大的改进.其实呢,merge into部分的update和u ...

  9. 使用grub硬盘重装ubuntu

    电脑一直是双系统,Windows8 + Ubuntu13.04,昨天瞎搞,更新了一下QT,结果就不能开机了...真心难. 老系统坏了也好,正好可以装个Ubuntu14.04LTS.手边没有U盘,可以把 ...

  10. ko list and css gradient

    <!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...