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) \\ ... ...
随机推荐
- jnhs-SpringMVC jsp页面向controller传递参数的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- 从Java到C++——union的使用方法
版权声明:本文为博主原创文章,未经博主同意不得用于不论什么商业用途,转载请注明出处. https://blog.csdn.net/luoweifu/article/details/33342965 你 ...
- NoSQL最新现状和趋势:云NoSQL数据库将成重要增长引擎
NoSQL最早起源于1998年,但从2009年开始,NoSQL真正开始逐渐兴起和发展.回望历史应该说NoSQL数据库的兴起,完全是十年来伴随互联网技术,大数据数据的兴起和发展,NoSQL在面临大数据场 ...
- Django与HTML业务基本结合--基本的用户名密码提交方法1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- System.getProperty()和getenv()
System.getproperty(String name) 获取系统属性 System.getProperties() 获取所有系统属性 System.getenv(String name) 获取 ...
- 一些windows server的操作
windows server2008R2的一些操作和arcgis9.3 服务和arcgis9.3安装 1.在虚拟机中安装: https://jingyan.baidu.com/article/0eb4 ...
- 蚁群算法MATLAB解TSP问题
Excel表exp12_3_1.xls中数据为: clc clear all [xdata,textdata]=xlsread('exp12_3_1.xls'); %加载20个城市的数据,数据按照表格 ...
- VisualVM介绍使用
1 打开VisualVM(这个工具放在JDK安装目录的bin目录下,双击jvisualvm.exe即可打开),如下图所示 以VisualVM自身为例,VisualVM本身也是一个java程序,当 ...
- PHP定时任务获取微信access_token的方法
一.使用brew安装php多版本方法 # brew install php56# brew install php70二.安装切换工具 # brew install php-version# sour ...
- WCF ChannelFactory
public static class WcfExtensions{ public static void Using<T>(this T client, Action<T&g ...