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) \\ ... ...
随机推荐
- 加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)
加载Selenium2library失败,检查D:\Python27\Lib\site-packages 目录下是否有Selenium2Library 目录,没有该目录,事情就尴尬了. 自己安装的版本 ...
- iOS播放器横竖屏切换
http://www.cocoachina.com/cms/wap.php?action=article&id=20292 http://feihu.me/blog/2015/how-to-h ...
- 【python之路20】函数作为参数
1.函数可以作为参数 1)函数名相当于变量指向函数 2)函数名后面加括号表示调用函数 #!usr/bin/env python # -*- coding:utf-8 -*- def f1(args): ...
- ML面试1000题系列(41-50)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 41. #include和#include“fi ...
- 理解async和await
async 是“异步”的简写,而 await 可以认为是 async wait 的简写. 所以应该很好理解 async 用于申明一个 function 是异步的,而 await 用于等待一个异步方法执 ...
- Linux下下载安装jdk1.7和IDEA
一.安装JDK1.7 准备: 到Oracle官网下载下载jdk1.7,参考博客 https://blog.csdn.net/H12KJGJ/article/details/79240984 官网地址: ...
- C++ 之手写memcpy
#include<iostream>#include<cstdio>using namespace std; void* mymemcpy(void* dst, const v ...
- js Date格式化时间兼容写法
var time = "2018-03-12 11:11:11".split(/[- : \/]/); date = new Date(time[0], time[1]-1, ti ...
- PLAY2.6-SCALA(十一) 模板常用场景
1.布局 声明一个views/main.scala.html模板作为主布局模板 @(title: String)(content: Html) <!DOCTYPE html> <ht ...
- vagrant up 时提示错误 cound not open file
根据教程:https://laravel-china.org/docs/laravel-development-environment/5.5/development-environment-wind ...