UVA 11754 Code Feat 中国剩余定理+暴力
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 中国剩余定理+暴力的更多相关文章
- UVA 11754 Code Feat 中国剩余定理+枚举
Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...
- UVA 11754 - Code Feat(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
- UVA 11754 Code Feat (枚举,中国剩余定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud C Code Feat The government hackers at C ...
- uva 11754 Code Feat (中国剩余定理)
UVA 11754 一道中国剩余定理加上搜索的题目.分两种情况来考虑,当组合总数比较大的时候,就选择枚举的方式,组合总数的时候比较小时就选择搜索然后用中国剩余定理求出得数. 代码如下: #includ ...
- Uva 11754 Code Feat
题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...
- UVA - 11754 Code Feat (分块+中国剩余定理)
对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...
- uva11754 中国剩余定理+暴力搜索
是当y的组合数较小时,暴力枚举所有组合,然后用中国剩余定理求每种组合的解,对解进行排序即可 注意初始解可能是负数,所以如果凑不够S个,就对所有解加上M,2M.... 当y的组合数较大时,选择一个k/x ...
- HDU1370Biorhythms(中国剩余定理||暴力)
Some people believe that there are three cycles in a person's life that start the day he or she is b ...
- UVA11754 - Code Feat
Hooray! Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed ...
随机推荐
- 网站开发常用jQuery插件总结(一)提示插件alertify
1.alertify插件功能 主要实现提示功能,用于代替js中的alert,confirm,prompt,显示友好的提示框 2.alertify官方地址 http://fabien-d.github. ...
- 少年Vince之遐想
本文999纯水贴,然转载仍需注明: 转载至http://www.cnblogs.com/VinceYang1994/ 昨天去姑姑家拜年,表哥房间的角落里有一架缠有蜘蛛网的遥控直升飞机. 打开飞机及遥控 ...
- [CSS]font- 属性
所有浏览器都支持 font 属性. 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持属性值 "inherit". 定义和用法 font 简写属性在一 ...
- 基于等待队列及poll机制的按键驱动代码分析和测试代码
按键驱动分析: #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> ...
- web2py--------------用web2py写 django的例子 --------建立一个投票应用(2)
建立模型 我们在models下及那里polls.py 文件内容如下 # -*- coding: utf-8 -*- pollsdb=DAL(uri='sqlite://polls.db') polls ...
- linux下修改IP信息
在Linux的系统下如何才能修改IP信息 以前总是用ifconfig修改,重启后总是得重做.如果修改配置文件,就不用那么麻烦了- A.修改ip地址 即时生效: # ifconfig eth0 192. ...
- 如何学习C++[转]
关于学C++, 我向你推荐一些书(当然能够结合课内项目实践更好) 1.The C++ Programming Language(Bjarne Stroustrup)2. Inside The C++ ...
- 安装ubuntu12.04LTS卡住以及花屏问题
昨天在XP下用grub4dos安装了ubuntu12.04LTS,总体上还算比较顺利,中途有碰到两个异常问题,解决了记录一下. 问题一:安装过程中读取ISO镜像文件时,卡在"checking ...
- 对CURL的一些研究
http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...
- javascript的族家族史
JavaScript 实现 完整的 JavaScript 实现是由以下 3 个不同部分组成的:ECMAScript.文档对象模型.浏览器对象模型.这也就是说 cocos2d-js 中 其实我们用的是 ...