UVA 11754

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

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set> using namespace std; typedef long long LL; const int N = ;
const int UB = ;
int X[N], C, S;
LL ans[UB], R[N];
set<int> Y[N];
#define ITOR iterator template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}
void gcd(LL a, LL b, LL &d, LL &x, LL & y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = , y = ;
}
LL lcm(LL a, LL b) { return a / gcd(a, b) * b;} LL cal() {
LL a = X[], r = R[] % a, d, x, y, tmp;
for (int i = ; i < C; i++) {
gcd(a, X[i], d, x, y);
if ((R[i] - r) % d) return -;
tmp = a * x * ((R[i] - r) / d) + r;
a = lcm(a, X[i]);
r = (tmp % a + a) % a;
}
return r ? r : a + r;
} void dfs(int p) {
if (p == C) {
LL tmp = cal();
if (~tmp) ans[++ans[]] = tmp;
return ;
}
set<int>::ITOR si;
for (si = Y[p].begin(); si != Y[p].end(); si++) {
R[p] = *si;
dfs(p + );
}
} void workdfs() {
LL LCM = ;
for (int i = ; i < C; i++) LCM = lcm(LCM, X[i]);
ans[] = ;
dfs();
sort(ans + , ans + ans[] + );
ans[] = (int) (unique(ans + , ans + ans[] + ) - ans - );
int mk = ans[];
while (ans[] < S) ans[ans[] + ] = ans[ans[] + - mk] + LCM, ans[]++;
} bool test(LL x) {
for (int i = ; i < C; i++) {
if (Y[i].find(x % X[i]) == Y[i].end()) return false;
}
return true;
} void workenum(int mk) {
set<int>::ITOR si;
ans[] = ;
for (int t = ; ans[] < S; t++) {
for (si = Y[mk].begin(); si != Y[mk].end() && ans[] < S; si++) {
LL cur = (LL) t * X[mk] + *si;
if ( cur && test(cur)) ans[++ans[]] = cur;
}
}
} int main() {
while (~scanf("%d%d", &C, &S) && (C || S)) {
int mk = , tt = , y, k;
bool enm = false;
for (int i = ; i < C; i++) {
scanf("%d%d", X + i, &k);
Y[i].clear();
for (int j = ; j <= k; j++) {
scanf("%d", &y);
Y[i].insert(y);
}
if (k * X[i] < k * X[mk]) mk = i;
tt *= k;
if (tt > UB) enm = true;
}
if (enm) workenum(mk);
else workdfs();
ans[] = min(ans[], (LL) S);
for (int i = ; i <= ans[]; i++) printf("%lld\n", ans[i]);
puts("");
}
return ;
}

——written by Lyon

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 中国剩余定理+暴力

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

  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 (枚举,中国剩余定理)

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

  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. UVa 11754 (中国剩余定理 枚举) Code Feat

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

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

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

  9. P4777 【模板】扩展中国剩余定理(EXCRT)&& EXCRT

    EXCRT 不保证模数互质 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\equiv b_2\ ({\rm mod}\ a_2) \\ ... ...

随机推荐

  1. Python之路,Day3- Python基础(转载Alex)

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  2. Leetcode33.Search in Rotated Sorted Array搜索旋转排序数组

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...

  3. CURL POST PHP

    function SendPostCurl($url,$post_data){ $curl = curl_init(); //初始化 curl_setopt($curl, CURLOPT_URL, $ ...

  4. 将jacoco集成到测试工具平台

    最近在做接口测试,想通过代码覆盖率来判断一下接口用例是否缺失,但是每次通过命令来生成覆盖率报告,感觉太麻烦,所以就想着把jacoco集成到测试工具平台中,只需要点几个按钮,就能查看到覆盖率报告. 测试 ...

  5. Vue--vue中的生命周期

    Vue的生命周期: 在理解vue生命周期前要把握它的三个重点: 创建-> 改变 -> 销毁 创建: 1.执行beforeCreate 2.监控data 3.注册事件 4.执行create ...

  6. web前端学习(二)html学习笔记部分(3)--range对象

    1.2.8  html5编辑api之range对象(一) 1.2.8.1  Range 对象基本概念 Range 对象的基本概念,通过使用 Range 对象所提供的方法实现一个鼠标选取内容,通过点击按 ...

  7. Sql基本知识回顾

    一. SQL 基本语句 SQL 分类: DDL —数据定义语言 (Create , Alter , Drop , DECLARE) DML —数据操纵语言 (Select , Delete , Upd ...

  8. Java项目压力测试(待补)

    JVM监控使用ava自带jvisualvm,在java安装目录jdk1.*/bin下(有很多更高级的东西 线程2000以下,太多切换太消耗.CPU使用率30%以下,更健壮

  9. Neo4j文档

    Neo4j 图数据库 主要有节点和关系,关系是有向边,节点和关系都有属性,属性是键值对 Neo4j使用CQL语句,代表Cypher查询语言 相当于SQL 统一声明 在所有的语句中均有结构 <no ...

  10. C++ 之手写memcpy

    #include<iostream>#include<cstdio>using namespace std; void* mymemcpy(void* dst, const v ...