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. jQuery Validate W3C内容

    导入 js 库 <script src="../js/jquery.js" type="text/javascript"></script&g ...

  2. 转载[WampServer下使用多端口访问]

    作者:韩子迟 原文链接:http://www.cnblogs.com/zichi/p/4589142.html 注意点:www和www2都需要安装服务: 在C:\wamp\bin\apache\Apa ...

  3. while和for可以相互转换例子

    //while和for循环可以相互转换,以下为简单格式: ;;) A; 等价于 : ) { A; ; } /* Name:while和for可以相互转换例子 Copyright: By.不懂网络 Au ...

  4. why-and-howto-calculate-your-events-per-second

    http://eromang.zataz.com/2011/04/12/why-and-howto-calculate-your-events-per-second/

  5. Database(Mysql)发版控制二

    author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...

  6. Hadoop MapReduce链式实践--ChainReducer

    版本号:CDH5.0.0,HDFS:2.3.0,Mapreduce:2.3.0,Yarn:2.3.0. 场景描写叙述:求一组数据中依照不同类别的最大值,比方,例如以下的数据: data1: A,10 ...

  7. SMACSS:一个关于CSS的最佳实践-3.Layout Rules

    本篇笔者要介绍的是Layout Rules.看完本篇,大家将会知道Layout Rules的作用,以及哪些CSS应该归类为Layout Rules. 什么是Layout Rules? Layout R ...

  8. css6种隐藏元素的方法

    6种方式可以隐藏一个元素: 1 CSS display的值是none.(该元素是不会在页面上显示)   2 type="hidden"的表单元素.(该元素是不会在页面上显示)   ...

  9. js中if的简写方法

    http://transitions1020.com/# 太帅! <script type="text/javascript"> 如果你想写 if (!false) { ...

  10. Problem E: Automatic Editing

    Problem E: Automatic EditingTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 3[Submit][Status ...