zoukankan      html  css  js  c++  java
  • UVA

    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);
    				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);
    				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("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    今晚直播 | 揭秘携程应用路由生态系统
    OpenStack虚拟云桌面在携程呼叫中心的应用
    从设计到开发,硅谷技术专家教你做“声控”APP
    深度学习系列 | 诺亚面向语音语义的深度学习研究进展
    知识图谱中的推理技术及其在高考机器人中的应用
    用户在线广告点击行为预测的深度学习模型
    知识库上的问答系统:实体、文本及系统观点
    深度学习在搜狗无线搜索广告中的应用
    元旦三天假期,实现一个电商退单管理系统【四】-手机客户端实现
    元旦三天假期,实现一个电商退单管理系统【三】
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7358905.html
Copyright © 2011-2022 走看看