uva 11754 Code Feat (中国剩余定理)
一道中国剩余定理加上搜索的题目。分两种情况来考虑,当组合总数比较大的时候,就选择枚举的方式,组合总数的时候比较小时就选择搜索然后用中国剩余定理求出得数。
代码如下:
#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 (中国剩余定理)的更多相关文章
- UVA 11754 Code Feat 中国剩余定理+枚举
Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...
- UVA 11754 Code Feat 中国剩余定理+暴力
lrj白书例题,真好 #include <stdio.h> #include <iostream> #include <vector> #include <m ...
- 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
题意概述: 有一个正整数$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小集合的方法输出答案.但 ...
- UVa 11754 (中国剩余定理 枚举) Code Feat
如果直接枚举的话,枚举量为k1 * k2 *...* kc 根据枚举量的不同,有两种解法. 枚举量不是太大的话,比如不超过1e4,可以枚举每个集合中的余数Yi,然后用中国剩余定理求解.解的个数不够S个 ...
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...
- P4777 【模板】扩展中国剩余定理(EXCRT)&& EXCRT
EXCRT 不保证模数互质 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\equiv b_2\ ({\rm mod}\ a_2) \\ ... ...
随机推荐
- MySQL笔记<一>创建数据库和表
参考博客 https://www.cnblogs.com/sqbk/p/5806797.html https://www.cnblogs.com/tomasman/p/7151962.html
- 2019.9.27 csp-s模拟测试53 反思总结
这个起名方式居然还有后续?! 为什么起名不是连续的?! T1想了半天,搞出来了,结果数组开小[其实是没注意范围].T2概率期望直接跳,后来翻回来写发现自己整个理解错了期望的含义[何].T3错误想到赛道 ...
- 【Ruby】与ruby相关的内容
本博文是为了记录Ruby相关的可学习资源,以便日常使用 在线学习Rails的网站版本 Ruby on Rails教程(Rails 5) Ruby的中文社区 Ruby China
- PHP把图片保存到数据库,将图片本身保存在数据库,而非保存路径
备注 百度开发者的云代码空间为了保证高可用,不允许用户将图片保存到代码空间中,使用CDN或者对象存储不仅收费而且使用比较复杂,于是考虑能否将img存储在数据库中,虽然很多人说会造成性能问题,权当一试 ...
- ACdream 1101 线段树
题目链接 瑶瑶想要玩滑梯 Time Limit: 10000/5000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) Submi ...
- [Vue CLI 3] Uglify 相关的应用和设计
在本文开始之前,先留一个问题? 如果在新版本我想加一个 drop_console 的配置呢? 在老版本的脚手架生成的配置中,对于线上环境的文件:webpack.prod.conf.js 使用了插件:u ...
- CSS3圆环旋转效果
html结构: <div class="demo"></div> css结构: .demo{ width:250px; height:250px; bord ...
- PHP核心编程--站内搜索
一. 站内搜索 前台页面: 在index.php页面中添加一个表单,输入搜索框 后台页面: 将index.php另存为search.php 对于搜索的 分页关键代码: 高亮关键字 相关 ...
- Hibernate的DetachedCriteria使用(含Criteria)转载
https://www.cnblogs.com/deng-cc/p/6428599.html 1.背景了解:Hibernate的三种查询方式 Hibernate总的来说共有三种查询方式:HQL.QBC ...
- Android书架实现
转自http://blog.csdn.net/wangkuifeng0118/article/details/7944215 书架效果: 下面先看一下书架的实现原理吧! 首先看一下layout下的布局 ...