UVA 11754 - Code Feat(数论)
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(数论)的更多相关文章
- UVA 11754 Code Feat (枚举,中国剩余定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud C Code Feat The government hackers at C ...
- uva 11754 Code Feat (中国剩余定理)
UVA 11754 一道中国剩余定理加上搜索的题目.分两种情况来考虑,当组合总数比较大的时候,就选择枚举的方式,组合总数的时候比较小时就选择搜索然后用中国剩余定理求出得数. 代码如下: #includ ...
- UVA 11754 Code Feat 中国剩余定理+枚举
Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...
- Uva 11754 Code Feat
题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...
- UVA 11754 Code Feat 中国剩余定理+暴力
lrj白书例题,真好 #include <stdio.h> #include <iostream> #include <vector> #include <m ...
- UVA - 11754 Code Feat (分块+中国剩余定理)
对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...
- UVA 11557 - Code Theft (KMP + HASH)
UVA 11557 - Code Theft 题目链接 题意:给定一些代码文本.然后在给定一个现有文本,找出这个现有文本和前面代码文本,反复连续行最多的这些文本 思路:把每一行hash成一个值.然后对 ...
- UVa 11754 (中国剩余定理 枚举) Code Feat
如果直接枚举的话,枚举量为k1 * k2 *...* kc 根据枚举量的不同,有两种解法. 枚举量不是太大的话,比如不超过1e4,可以枚举每个集合中的余数Yi,然后用中国剩余定理求解.解的个数不够S个 ...
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
随机推荐
- discuz门户文章页面模板修改
修改内容:view.htm 1.文章标题,模板代码 <h1 class="ph">$article[title] <!--{if $article['status ...
- MockObject
http://www.mockobjects.com/ http://jmock.org/download.html https://jakarta.apache.org/cactus/mock_vs ...
- HDU 5877 Weak Pair(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5877 [题目大意] 给出一棵带权有根树,询问有几对存在祖先关系的点对满足权值相乘小于等于k. [题 ...
- (Android) Chinese Character
Convert Chinese strings to English strings Apply pinyin4j.jar public static class ConvertChineseToPi ...
- ExpandableListView(三)只展开一个group,没有child不展开group
本文是自己在实践中,发现的问题. 有时候想让界面更加的人性化,就要实现很多的效果,比如只展开一个group,在点击下个group的同时,关闭之前的group 在一个ExpandableListView ...
- hdu4491 Windmill Animation(计算几何)
Windmill Animation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- java字符串函数及理解
Java中的字符串也是一连串的字符.但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理.将字符串作为内置的对象处理允许Java提供十分丰富的功能特性 ...
- swift菜鸟入门视频教程-03-字符串和字符
本人自己录制的swift菜鸟入门,欢迎大家拍砖.有什么问题能够在这里留言. 主要内容: 字符串字面量 初始化空字符串 字符串可变性 字符串是值类型 使用字符 计算字符数量 连接字符串和字符 字符串插值 ...
- Java经典问题算法大全
/*[程序1] 题目:古典问题:有一对兔子.从出生后第3个月起每一个月都生一对兔子.小兔子长到第三个月后每一个月又生一对兔子,假如兔子都不死.问每一个月的兔子总数为多少? 1.程序分析: 兔子的规律 ...
- 线性规划问题的matlab求解
函数:[x, fval] = linprog(f, A, b, Aeq, Beq, LB, UB) 返回的x:是一个向量——在取得目标函数最小时各个xi的取值: 返回的fval:目标函数的最小值: 参 ...