標籤:des os io strong for ar art div cti
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 <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <map>#include <vector>#include <set>typedef long long ll;using namespace std;const int maxc = 9;const int maxk = 100;const int LIMIT = 10000;set<int> values[maxc];vector<ll> 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<ll> 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;}
UVA - 11754 Code Feat