uva 11754 Code Feat (中国剩余定理)

时间:2023-03-09 23:32:14
uva 11754 Code Feat (中国剩余定理)

UVA 11754

  一道中国剩余定理加上搜索的题目。分两种情况来考虑,当组合总数比较大的时候,就选择枚举的方式,组合总数的时候比较小时就选择搜索然后用中国剩余定理求出得数。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set> using namespace std; typedef long long LL; const int N = ;
const int UB = ;
int X[N], C, S;
LL ans[UB], R[N];
set<int> Y[N];
#define ITOR iterator template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}
void gcd(LL a, LL b, LL &d, LL &x, LL & y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = , y = ;
}
LL lcm(LL a, LL b) { return a / gcd(a, b) * b;} LL cal() {
LL a = X[], r = R[] % a, d, x, y, tmp;
for (int i = ; i < C; i++) {
gcd(a, X[i], d, x, y);
if ((R[i] - r) % d) return -;
tmp = a * x * ((R[i] - r) / d) + r;
a = lcm(a, X[i]);
r = (tmp % a + a) % a;
}
return r ? r : a + r;
} void dfs(int p) {
if (p == C) {
LL tmp = cal();
if (~tmp) ans[++ans[]] = tmp;
return ;
}
set<int>::ITOR si;
for (si = Y[p].begin(); si != Y[p].end(); si++) {
R[p] = *si;
dfs(p + );
}
} void workdfs() {
LL LCM = ;
for (int i = ; i < C; i++) LCM = lcm(LCM, X[i]);
ans[] = ;
dfs();
sort(ans + , ans + ans[] + );
ans[] = (int) (unique(ans + , ans + ans[] + ) - ans - );
int mk = ans[];
while (ans[] < S) ans[ans[] + ] = ans[ans[] + - mk] + LCM, ans[]++;
} bool test(LL x) {
for (int i = ; i < C; i++) {
if (Y[i].find(x % X[i]) == Y[i].end()) return false;
}
return true;
} void workenum(int mk) {
set<int>::ITOR si;
ans[] = ;
for (int t = ; ans[] < S; t++) {
for (si = Y[mk].begin(); si != Y[mk].end() && ans[] < S; si++) {
LL cur = (LL) t * X[mk] + *si;
if ( cur && test(cur)) ans[++ans[]] = cur;
}
}
} int main() {
while (~scanf("%d%d", &C, &S) && (C || S)) {
int mk = , tt = , y, k;
bool enm = false;
for (int i = ; i < C; i++) {
scanf("%d%d", X + i, &k);
Y[i].clear();
for (int j = ; j <= k; j++) {
scanf("%d", &y);
Y[i].insert(y);
}
if (k * X[i] < k * X[mk]) mk = i;
tt *= k;
if (tt > UB) enm = true;
}
if (enm) workenum(mk);
else workdfs();
ans[] = min(ans[], (LL) S);
for (int i = ; i <= ans[]; i++) printf("%lld\n", ans[i]);
puts("");
}
return ;
}

——written by Lyon