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 ...
随机推荐
- hexo博客部署到github无法上传的问题
博客生成之后,按照网上别人的教程,讲项目部署到github上,修改_config.yaml中的deploy部分如下所示: deploy: type: git repository: https://g ...
- 通过netty实现服务端与客户端的长连接通讯,及心跳检测。
基本思路:netty服务端通过一个Map保存所有连接上来的客户端SocketChannel,客户端的Id作为Map的key.每次服务器端如果要向某个客户端发送消息,只需根据ClientId取出对应的S ...
- POJ 1743 Musical Theme(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...
- LINQ to SQL的CRUD操作
创建数据对象模型 sqlmetal /code:"C:\MyProjects\VS2008\Data\LinqConsoleApp2\LinqConsoleApp2\northwnd.cs& ...
- ubuntu12.04下 安装虚拟主机
Ubuntu Linux 方法一 一.修改/etc/apache2/sites-available/ 1. 打开目录 /etc/apache2/sites-available/, 发现 default ...
- MongoDB(三)mongoDB下载和安装
软件下载 下载mongodb最新的包:http://www.mongodb.org/downloads 下载mongodb可视化界面,mongoVUE:http://download.csdn.net ...
- SharePoint 2007 (MOSS/WSS) - how to remove "Download a Copy" context menu from a Document Library
One of my friend and colleague asked me this question. I found it tricky and a good post for my blog ...
- WPF 实现的等待效果界面
这个界面的效果是从WinForm 转变过来,可以实现等待的效果,操作完成以后就自动关掉. 效果图如下 有一个动态的手机等待效果的样式,中间的文字可以自己定义,提供了方法可以修改中间"正在加载 ...
- C++之构造函数重载
#include<stdio.h> class Test { private: int i; int j; int k; ...
- 使用 IObjectSafety 标记 ATL 控件初始化的安全
MSDN原文.这里我将代码使用到了BHO里面,运行调试没问题.拿来分享一下 概要 您可以使用 IObjectSafetyImpl 的默认实现来标记为可安全执行脚本的控件.在许多情况下,您需要将标记为可 ...