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 ...
随机推荐
- appcan 跨窗口处理方法 appcan.window.evaluateScript({name,scriptContent,type})使用解读
appcan.window.evaluateScript({ name,/*主窗口名称,此窗口要先用appcan.window.open打开了,才能找到,此方法才会有效*/ scriptContent ...
- 自定义弹出div对话框
<style type="text/css"> html,body{height:100%;overflow:hidden;} body,div,h2{margin:0 ...
- 安装Android Studio报failed to find java version for 'C:\windows\system32\java.exe':[2] The system cannot find the specified file.错误的解决方案
方案很简单,找到SYSTEM32目录下的java.exe文件,重命名为java.exe.orj. 方案出处:http://stackoverflow.com/questions/10339679/an ...
- WINIO64位模拟键鼠操作
最近玩游戏有点入迷,心急升级经验,于是某天开始"挂尸体",一系列手动操作之后决定写一个外挂,模拟一系列键盘和鼠标操作,没做过游戏winform也用得少,开始以为只要简单调用winA ...
- MVC 4 网页版发送 邮件的配置问题
有时项目要用到邮箱验证就要发送邮件传统的解决方案: public void SendResetPasswordEmail(string email) { MailAddress from = new ...
- Java使用memcached
1.加载commons-pool-1.5.6.jar.java_memcached-release_2.6.6.jar.slf4j-api-1.6.1.jar.slf4j-simple-1.6.1.j ...
- 异步IO模型和Overlapped结构
.NET中的 Overlapped 类 异步IO模型和Overlapped结构(http://blog.itpub.net/25897606/viewspace-705867/) 数据结构 OVERL ...
- WCF 消息压缩性能问题及解决方法
最近使用WCF作为通迅框架开发一套信息系统,系统使用传统C/S框架,系统有可能会部署在互联网上,因此决定对传输的数据进行GZIP压缩,原来在使用.NET Remoting时,可以使用插入自定义的Cha ...
- 一个用于清除loadrunner产生log文件的批处理
@echo off set work_path="%~dp0" for /R %%s in (*.txt,*.log) do ( del /f "%%s" ) ...
- SGU 488 Dales and Hills
这给题目和LIS类似,只不过是求连续的单调序列,用单调队列可破之,比如求LDIS(连续单增序列),如果a[i]大于栈顶元素入栈,将top作为序列长度,反过来再扫一遍就是包含该元素的单调递减序列,这样通 ...