如果直接枚举的话,枚举量为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的更多相关文章

  1. UVA 11754 Code Feat 中国剩余定理+枚举

    Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...

  2. UVA 11754 Code Feat (枚举,中国剩余定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at C ...

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

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

  4. UVA 11754 - Code Feat(数论)

    UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...

  5. Uva 11754 Code Feat

    题意概述: 有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质, 你的任务是找出最小的S个解. ...

  6. UVA 11754 (暴力+中国剩余定理)

    题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...

  7. UVA11754 - Code Feat

    Hooray!  Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed ...

  8. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  9. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

随机推荐

  1. 得到当前活动的controller

    UIWindow+PazLabs.h (header file) #import <UIKit/UIKit.h> @interface UIWindow (PazLabs) - (UIVi ...

  2. IntelliJ IDEA 15 设置默认浏览器

    一.设置系统默认浏览器 二.设置项目启动默认浏览器

  3. 把工程部署在tomcat的root路径下

    myeclipse可以右键工程:(eclipse也可以)选择properties->myeclipse->web:把web context-root改成:/然后在用myeclipse部署项 ...

  4. 修改tomcat 启动45秒

    当我们需要增加Tomcat的启动时间,修改方法如下:

  5. HtmlAgilityPack 简单运用

    WebClient client = new WebClient(); MemoryStream ms = new MemoryStream(client.DownloadData("htt ...

  6. 找不到对应的webservice配置参数[ProcessService]

    在UI端 保存时 界面显示无法保存 且报此错误 “找不到对应的webservice配置参数[ProcessService]” 此下为解决方法: 首先 在[应用管理平台]--[参数模板设置] 找到你的参 ...

  7. 能"干掉"苹果的中国"黑客"

    他是全球发现苹果漏洞最多的人,他曾穷的住在小黑屋,他经常接到国家安全部门的电话,他差点堵住周鸿祎的路,他是谁? 无名英雄 我们最终还是没有见到吴石本人,即便他的生意合伙人刘盛(化名)已经应承了帮我们牵 ...

  8. How to Enable 64-bit Processes for Enhanced Protected Mode in Internet Explorer 11 (IE11)

       Information Enhanced Protected Mode (EPM) adds additional security to Protected Mode and includes ...

  9. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  10. 【hadoop2.6.0】MapReduce原理

    看了几篇博文,感觉还是云里雾里的. http://blog.csdn.net/opennaive/article/details/7514146 http://www.aboutyun.com/thr ...