UVA 11754 - Code Feat

题目链接

题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中

思路:LRJ大白例题,分两种情况讨论

1、全部x之积较小时候,暴力枚举每一个集合选哪个y。然后中国剩余定理求解

2、全部x之积较大时候,选定一个k/x尽可能小的序列,枚举x * t + y (t = 1, 2, 3...)去暴力求解。

代码:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
#include <algorithm>
using namespace std; const int N = 15;
const int M = 105; int c, s, x[N], k[N], y[N][M], now;
set<int> value[N];
vector<long long> ans;
long long a[N]; void solve_enum() {
for (int i = 0; i < c; i++) {
if (c == now) continue;
value[i].clear();
for (int j = 0; j < k[i]; j++)
value[i].insert(y[i][j]);
}
for (int t = 0; ; t++) {
for (int i = 0; i < k[now]; i++) {
long long n = (long long)x[now] * t + y[now][i];
if (n == 0) continue;
bool ok = true;
for (int i = 0; i < c; i++) {
if (i == now) continue;
if (!value[i].count(n % x[i])) {ok = false; break;}
}
if (ok) {printf("%lld\n", n); if (--s == 0) return;}
}
}
} long long exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {x = 1; y = 0; return a;}
long long d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
} long long china() {
long long M = 1, ans = 0;
for (int i = 0; i < c; i++)
M *= x[i];
for (int i = 0; i < c; i++) {
long long w = M / x[i];
long long xx, yy;
exgcd(x[i], w, xx, yy);
ans = (ans + w * yy * a[i]) % M;
}
return (ans + M) % M;
} void dfs(int d) {
if (d == c) {
ans.push_back(china());
return;
}
for (int i = 0; i < k[d]; i++) {
a[d] = y[d][i];
dfs(d + 1);
}
} void solve_china() {
ans.clear();
dfs(0);
sort(ans.begin(), ans.end());
long long M = 1;
for (int i = 0; i < c; i++) M *= x[i];
for (int i = 0; ; i++) {
for (int j = 0; j < ans.size(); j++) {
long long n = M * i + ans[j];
if (n > 0) {printf("%lld\n", n); if (--s == 0) return;}
}
}
} int main() {
while (~scanf("%d%d", &c, &s) && s || c) {
now = 0;
long long sum = 1;
for (int i = 0; i < c; i++) {
scanf("%d%d", &x[i], &k[i]);
sum *= k[i];
if (k[i] * x[now] < k[now] * x[i])
now = i;
for (int j = 0; j < k[i]; j++)
scanf("%d", &y[i][j]);
sort(y[i], y[i] + k[i]);
}
if (sum > 10000) solve_enum();
else solve_china();
printf("\n");
}
return 0;
}

UVA 11754 - Code Feat(数论)的更多相关文章

  1. UVA 11754 Code Feat (枚举,中国剩余定理)

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

  2. uva 11754 Code Feat (中国剩余定理)

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

  3. UVA 11754 Code Feat 中国剩余定理+枚举

    Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...

  4. Uva 11754 Code Feat

    题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...

  5. UVA 11754 Code Feat 中国剩余定理+暴力

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

  6. UVA - 11754 Code Feat (分块+中国剩余定理)

    对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...

  7. UVA 11557 - Code Theft (KMP + HASH)

    UVA 11557 - Code Theft 题目链接 题意:给定一些代码文本.然后在给定一个现有文本,找出这个现有文本和前面代码文本,反复连续行最多的这些文本 思路:把每一行hash成一个值.然后对 ...

  8. UVa 11754 (中国剩余定理 枚举) Code Feat

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

  9. UVA 10627 - Infinite Race(数论)

    UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...

随机推荐

  1. 保存属性至xml并读取

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  2. python学习【一】基础入门

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  3. java反编译命令javap

    1. 输出所有类和成员 javap -private XX.class 2. 输出分解后的代码 javap -c XX.class

  4. H3C交换配置PBR最佳实践

    简要说明 PBR算是比较常用的功能,需要我们去掌握一下 配置步骤简要 配置BFD 配置track 配置acl 配置policy-based-route 在接口上面应用policy-based-rout ...

  5. Windows系统下远程Linux系统

    Windows系统下远程Linux系统 工具:Xmanager 启动界面: 配置保存路径(win7): C:\Users\Administrator\AppData\Roaming\NetSarang ...

  6. Windows 安装 psutil

    第一步:下载pustil 网址:https://pipy.python.org  (http://yunpan.cn/cJg8aQpYwqfzh (提取码:7fa7)) 第二步:安装这个就没有什么好说 ...

  7. MOSS 2010 无法同步用户配置文件

    The management agent “MOSSAD-Synch AD Connection” failed on run profile “DS_DELTAIMPORT” because of ...

  8. 浅谈qt 布局器

    在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...

  9. andrewchilds/jQuery.DomOutline

    andrewchilds/jQuery.DomOutline DOM - 使用Javascript:让用户选择一个类似Firebug的HTML元素? -

  10. JAVA异常设计原则

    异常是面向对象语言非常重要的一个特性,良好的异常设计对程序的可扩展性.可维护性.健壮性都起到至关重要. JAVA根据用处的不同,定义了两类异常     * Checked Exception: Exc ...