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 ...
随机推荐
- hdu 4850 Wow! Such String! 欧拉回路
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...
- Eat that Frog
Eat that Frog,中文翻译过来就是“吃掉那只青蛙”.不过这里并不是讨论怎么去吃青蛙,而是一种高效的方法. Eat that Frog是Brian Tracy写的一本书(推荐阅读).这是一个很 ...
- 安装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 ...
- Nodejs初学者福音
Nodejs+Express+MongoDb 搭建个人博客 001 我喜欢把任务或者工作分解成工作流来完成,如下,后面将会按照流程来详述,希望能为Nodejs初学者及推广Nodejs做出些努力. n ...
- 《编写高质量代码:改善Python程序的91个建议》读后感
编写高质量代码:改善Python程序的91个建议 http://book.douban.com/subject/25910544/ 1.(建议16)is 用于判断两个对象的id是否相等,==才是判断 ...
- WPF中Application.Current的使用
WPF程序对应一个Application对象,当前的Application对象可以通过Application.Current获取,通过获取到的Application对象,我们可以做以下的事情: App ...
- 在Linux系详解Linux bash中的变量
(大讲台:国内首个it在线教育混合式自适应学习) 统中进行日常运维或者是编写脚本时,变量是再熟悉不过的了,但这些变量都有哪些类型,具体的用法又有哪些差异呢?本文整理分享给大家: 一.bash变量类型: ...
- 查看uCOS-II的CPU使用率
代码模板: void main(void) { OSInit(); /* 安装uCOS-II的任务切换向量 */ /* 创建用户起始任务TaskStart */ OSStart(); } void T ...
- asp.net中下载功能
//流方式下载 protected void ButtonButtonDownload_Click(object sender, EventArgs e) { //string fileName = ...
- 用JavaScript获取一个超链接的绝对URL地址
对于Web程序员来说,处理简单的URL格式也许会成为一场噩梦.试想一下,一个网址里有很多组成部分都会影响你对它的解析方法: 是否以/字符开头 是否以//开头 是否以?号开头 是否以#号开头 …等等 当 ...