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因 ...
随机推荐
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- C#WinForm中显示实时时间:年/月/日 时/分/秒 星期X
//加载窗体时 string weekstr = ""; private void Form22_Load(object sender, EventArgs e) { this.t ...
- 在MAC上安装虚拟机搭建Ubuntu开发环境
由于工作需要,需要在LINUX环境搭建服务器,但是工作中使用的是MAC系统,只好用虚拟机来搭建LINUX服务器环境.下面记录介绍一下搭建步骤以供需要的人参考使用. 下载准备 虚拟机使用VMWare ...
- 完整实例(C# Socket)
问题描述: 现在创建一个C# Socket实例,客户端断开服务器能立刻输出断开连接客户端信息 服务器端断开,客户端能立刻察觉服务器状态 问题解决: 服务器端代码: 客户端代码: 以上 ...
- transparent 的新问题
http://msdn.microsoft.com/en-us/library/windows/desktop/bb172255(v=vs.85).aspx 人物透明 显示是纹理和白色混合 开始怀疑是 ...
- ios开发小技巧之摇一摇截屏
1. 监控摇一摇动作 1> 让当前视图控制器成为第一响应者 // 必须先让当前视图控制器成为第一响应者才能响应动作时间 [self becomeFirstResponder]; 2> 实现 ...
- CString 转换成 char *
最近用到CString类,转换成 char * 类型,下面介绍用法: 一.CString 和 LPSTR 转换: CString转换成LPSTR: 方法一:CString server; LPSTR ...
- 解决jquery版本冲突问题
解决jQuery1.3.2和1.4.2的冲突.(测试通过) 第一步:在1.4.2的源代码的最后加上一句 var $j4 = jQuery.noConflict(true);//之所以在源码这里加,而不 ...
- jquery中判断是否按下回车enter键
<script> function sendsubmit() { $("#userLoginForm").submit(); return false; ...
- PHP 打印函数之 print print_r
print 说明 int print ( string $arg ) 输出 arg print 实际上不是一个函数(它是一个语言结构),因此你可以不必使用圆括号来括起它的参数列表 参数 arg:输入数 ...