UVa 11754 (中国剩余定理 枚举) Code Feat
如果直接枚举的话,枚举量为k1 * k2 *...* kc
根据枚举量的不同,有两种解法。
- 枚举量不是太大的话,比如不超过1e4,可以枚举每个集合中的余数Yi,然后用中国剩余定理求解。解的个数不够S个的时候,要把这些解分别加上M、2M...(M = X1 * X2 *...* Xc)
- 如果上述枚举量太大的话,直接枚举x。找一个k/X最小的条件,然后让x = t * X + Yi开始枚举,因为这样枚举x增长得最快,所以枚举次数也就最少。如果符合要求的话则输出。
上面两种方法都要注意所找到的解为正整数。
typedef long long LL; void gcd(LL a, LL b, LL& d, LL& x, LL& y)
{
if(!b) { d = a; x = ; y = ; }
else { gcd(b, a%b, d, y, x); y -= x*(a/b); }
} LL china(int n, int* a, int* m)
{
LL M = , d, y, x = ;
for(int i = ; i < n; i++) M *= m[i];
for(int i = ; 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;
} #include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std; const int maxc = ;
const int maxk = ;
const int LIMIT = ;
int C, S;
int bestc;
int X[maxc], Y[maxc][maxk], k[maxc];
set<int> values[maxc]; void solve_enum()
{
for(int c = ; c < C; c++) if(c != bestc)
{
values[c].clear();
for(int i = ; i < k[c]; i++)
values[c].insert(Y[c][i]);
} for(int t = ; S != ; t++)
{
for(int i = ; i < k[bestc]; i++)
{
LL n = (LL)t * X[bestc] + Y[bestc][i];//枚举解
if(n == ) continue;
bool ok = true;
for(int c = ; c < C; c++) if(c != bestc)//判断是否符合要求
if(!values[c].count(n % X[c])) { ok = false; break; }
if(ok) { printf("%lld\n", n); if(--S == ) { break; } }
}
}
} int a[maxc];
vector<LL> sol; void dfs(int d)
{
if(d == C) sol.push_back(china(C, a, X));
else for(int i = ; i < k[d]; i++)
{
a[d] = Y[d][i];
dfs(d + );
}
} void solve_china()
{
sol.clear();
dfs();
sort(sol.begin(), sol.end()); LL M = ;
for(int i = ; i < C; i++) M *= X[i]; for(int i = ; S != ; i++)
{
for(int j = ; j < sol.size(); j++)
{
LL n = M * i + sol[j];
if(n == ) continue;
printf("%lld\n", n);
if(--S == ) break;
}
}
} int main()
{
freopen("in.txt", "r", stdin); while(scanf("%d%d", &C, &S) == && C && S)
{
bestc = ;
int tot = ;
for(int c = ; c < C; c++)
{
scanf("%d%d", &X[c], &k[c]);
tot *= k[c];
if(k[c] * X[bestc] < k[bestc] * X[c]) bestc = c;
for(int i = ; i < k[c]; i++)
scanf("%d", &Y[c][i]);
sort(Y[c], Y[c] + k[c]);
} if(tot > LIMIT) solve_enum();
else solve_china();
printf("\n");
} return ;
}
代码君
UVa 11754 (中国剩余定理 枚举) Code Feat的更多相关文章
- UVA 11754 Code Feat 中国剩余定理+枚举
Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...
- 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(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
- Uva 11754 Code Feat
题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...
- UVA11754 - Code Feat
Hooray! Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
随机推荐
- Java 集合类(一)
今天我们先讲一下Collection: Collection和Collections的区别: java.util.Collection是一种java集合接口,它提供了对集合对象的基本操作通用接口方法, ...
- 【递推】BZOJ 1088: [SCOI2005]扫雷Mine
1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2275 Solved: 1328[Submit][St ...
- [nowCoder] 局部最小值位置
定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小:如果arr[N-1]<arr[ ...
- POJ 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6044 Accepted: 24 ...
- django如何用orm增加manytomany关系字段(自定义表名)
不自定义表名的,网上有现成的,但如果自定义之后,则要变通一下了. app_insert = App.objects.get(name=app_name) site_insert = Site.obje ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- java四种数组排序
数组的四种排序 1.快速排序法Arrays.sort(); 用法1.sort(byte[] a) 对指定的 byte 型数组按数字升序进行排序. sort(byte[] a, int fro ...
- ExecutorService中submit和execute的区别
在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过Executor来启动线程比用Thread的start()更好.在新特征中 ...
- ADO.NET入门教程(二)了解.NET数据提供程序
出处:http://www.cnblogs.com/liuhaorain/archive/2012/02/11/2346312.html 1. 什么是.NET数据提供程序? .NET Framewor ...
- 设计模式之工厂方法模式VS简单工厂方法模式
名词解释: 简单工厂:这个实在是没什么解释的,就是一个工厂类,然后有一个方法,根据传递的参数可以通过switch(你也可以是if,或者是使用高端的反射 )来进行对象的创建. 工厂方法:定义一个用于创建 ...