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. linux杂记(十一)Bash Shell的使用环境

    Bash Shell使用环境 Bash Shell使用环境 1.登录讯息显示数据:/etc/issue,/etc/motd 我们在终端机接口(tty1~tty6)登入的时候,会有几行提示的字符串,那个 ...

  2. Apache 开启 Https

    1. 准备所需工具: 1) apache httpd2.4 浏览 2) Win32 OpenSSL v1.0.2d 浏览 2. 安装 2.1 安装Apache2.4服务 php环境搭建 浏览 2.2 ...

  3. linux 使用者管理

    1.用户标识符 UID  用户ID GID  用户组ID 2./etc/passwd 文件结构 id范围:0系统管理员|1~499 (系统账号)|500~65535 可登录账号

  4. 投资新兴市场和细分市场 good

    新兴市场对程序员来说,就是一种新的语言.一个新的平台.一套新的框架.新兴市场因为刚刚兴起,所以几乎所有人都在同一个起跑线,特别适合后进者.我认识从一个2011年开始学习iOS开发的同学,他能能力中等, ...

  5. VC++或QT下 高精度 多媒体定时器

    在VC编程中,用SetTimer可以定义一个定时器,到时间了,就响应OnTimer消息,但这种定时器精度太低了.如果需要精度更高一些的定时器(精 确到1ms),可以使用下面的高精度多媒体定时器进行代码 ...

  6. perl 爬取同花顺数据

    use LWP::UserAgent; use utf8; use DBI; $user="root"; $passwd='xxx'; $dbh=""; $db ...

  7. J2EE项目中异常处理

     为什么要在J2EE项目中谈异常处理呢?可能许多java初学者都想说:“异常处理不就是try….catch…finally吗?这谁都会啊!”.笔者在初学java时也是这样认为的.如何在一个多层的j2e ...

  8. js 定义类对象

    //定义类     //方式一     function A_class(arg1,arg2){         this.arg1=arg1;         this.arg2=arg2;     ...

  9. 关于windows服务的操作

    /// <summary> /// 判断是否安装了某个服务 /// </summary> /// <param name="serviceName"& ...

  10. c 查找A字符串在B字符串中是否存在,计算出现的次数

    主要是应用了头文件<string.h>中的strstr函数 char * strstr(const char *s1, const char *s2); 查找是否存在: #include& ...