博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA - 11754 Code Feat
阅读量:6985 次
发布时间:2019-06-27

本文共 3931 字,大约阅读时间需要 13 分钟。

Hooray!  Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed the moles in the government,prevented an environmental catastrophe, and found homes for three orphanedkittens, all in the span of 19 consecutive hours.  But now, he only has 5 hours remaining todeal with his final challenge: an activated nuclear bomb protected by asecurity code.  Can you help him figureout the code and deactivate it?  Eventsoccur in real time.

The governmenthackers at CTU (Counter-Terrorist Unit) have learned some things about thecode, but they still haven't quite solved it.They know it's a single, strictly positive, integer.  They also know several clues of the form "whendivided by X, the remainder is one of {Y1, Y2, Y3, ...,Yk}".There are multiple solutions to these clues, but the code is likely tobe one of the smallest ones.  So they'dlike you to print out the first few solutions, in increasing order.

The world iscounting on you!

Input

Input consistsof several test cases.  Each test casestarts with a line containing C, the number of clues (1 <= C <= 9), andS, the number of desired solutions (1 <= S <= 10).  The next C lines each start with two integersX (2 <= X) and k (1 <= k <= 100), followed by the k distinct integersY1, Y2, ..., Yk (0 <= Y1,Y2, ..., Yk < X).

You may assumethat the Xs in each test case are pairwise relativelyprime (ie, they have no common factor except 1).  Also, the product of the Xs will fit into a32-bit integer.

The last testcase is followed by a line containing two zeros.

Output

For each testcase, output S lines containing the S smallest positive solutions to the clues,in increasing order.

Print a blankline after the output for each test case.

Sample Input                              

Sample Output

 

3 2

2 1 1

5 2 0 3

3 2 1 2

0 0

5

13

 

 

Problem Setter: Derek Kisman, Special Thanks: Samee Zahur

题意:有一个正整数N满足C个条件,每一个条件都如“它除以X的余数在集合{Y1,Y2...YK}中”,全部条件中的X两两互素。求最小的S个解

思路:刘汝佳入门经典的例题。两种思路,当余数组合的可能性太大的话,我们用枚举搜索的方法,否则就是採用中国剩余定理的方法。枚举搜索的方法是:我们知道可能的答案一定是满足某个条件的某个余数的,所以我们枚举这个,为了加高速度,我们找X尽量大,并且K尽量小的,也就是找X/k最大的

#include 
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;const int maxc = 9;const int maxk = 100;const int LIMIT = 10000;set
values[maxc];vector
sol; int C, X[maxc], k[maxc];int Y[maxc][maxk];int a[maxc];void solve_enum(int S, int bc) { for (int c = 0; c < C; c++) if (c != bc) { values[c].clear(); for (int i = 0; i < k[c]; i++) values[c].insert(Y[c][i]); } for (int t = 0; S != 0; t++) { for (int i = 0; i < k[bc]; i++) { ll n = (ll) X[bc]*t + Y[bc][i]; if (n == 0) continue; int ok = 1; for (int c = 0; c < C; c++) if (c != bc) if (!values[c].count(n % X[c])) { ok = 0; break; } if (ok) { printf("%lld\n", n); if (--S == 0) break; } } }}void gcd(ll a, ll b, ll &d, ll &x, ll &y) { if (!b) { d = a, x = 1, y = 0; } else { gcd(b, a%b, d, y, x); y -= x*(a/b); }}ll china(int n, int *a, int *m) { ll M = 1, d, y, x = 0; for (int i = 0; i < n; i++) M *= m[i]; for (int i = 0; i < n; i++) { ll w = M / m[i]; gcd(m[i], w, d, d, y); x = (x + y*w*a[i]) % M; } return (x + M) % M;}void dfs(int dep) { if (dep == C) sol.push_back(china(C, a, X)); else for (int i = 0; i < k[dep]; i++) { a[dep] = Y[dep][i]; dfs(dep+1); }}void solve_china(int S) { sol.clear(); dfs(0); sort(sol.begin(), sol.end()); ll M = 1; for (int i = 0; i < C; i++) M *= X[i]; vector
ans; for (int i =0; S != 0; i++) { for (int j = 0; j < sol.size(); j++) { ll n = M * i + sol[j]; if (n > 0) { printf("%lld\n", n); if (--S == 0) break; } } }}int main() { int S; while (scanf("%d%d", &C, &S) != EOF && C) { ll tot = 1; int bestc = 0; for (int c = 0; c < C; c++) { scanf("%d%d", &X[c], &k[c]); tot *= k[c]; for (int i = 0; i < k[c]; i++) scanf("%d", &Y[c][i]); sort(Y[c], Y[c]+k[c]); if (k[c]*X[bestc] < k[bestc]*X[c]) bestc = c; } if (tot > LIMIT) solve_enum(S, bestc); else solve_china(S); printf("\n"); } return 0;}

转载地址:http://bgvpl.baihongyu.com/

你可能感兴趣的文章
Mysql中使用命令行导入.sql文件新建数据库表(图文)
查看>>
RUBY有感
查看>>
spring 配置多数据源
查看>>
Java 线程数据交换控制器Exchange使用实例
查看>>
IBM X系列服务器IMM日志采集
查看>>
实验三 静态路由、默认路由配置
查看>>
mysql 查看导出数据字典
查看>>
linux命令--cp
查看>>
到底怎么样才叫看书?
查看>>
python 将ipv4的格式转换
查看>>
C语言宏的副作用的简单实例
查看>>
关于C语言结构体对齐的学习
查看>>
富文本框
查看>>
windows下安装rabbitMQ
查看>>
20个优秀的移动(iPhone)网站设计案例
查看>>
week04_python函数返回值、作用域
查看>>
CentOS 6.3安装Nginx开启目录浏览、下载功能
查看>>
oracle登陆认证方式
查看>>
FMDB/SQLCipher数据库管理
查看>>
cocos_python
查看>>