Uva 11754 Code Feat
题意概述:
有一个正整数$N$满足$C$个条件,每个条件都形如“它除以$X$的余数在集合$\{Y_1, Y_2, ..., Y_k\}$中”,所有条件中的$X$两两互质,
你的任务是找出最小的S个解。
数据范围:
$1\leq C\leq9, 1 \leq S \leq 10, X \geq 2, 1 \leq k \leq 100, 0 \leq Y_i \leq X$
分析:
如果每个集合元素个数为1,那么我们直接使用中国剩余定理求解即可。
因此我们想到枚举余数,但是余数的组合最多会有$100^9$种可能,太多了。我们发现加入$k$的积不大的话,使用这种方法是可行的。比如
我们假设每个集合元素都不超过5。而当集合元素增加时,由于可行解是在模$M = \prod X_i$下的在枚举复杂度提高的同时也意味着可行解在
数轴上变得稠密,这意味着我们可以通过从小到大枚举答案并判定的方法解决。为了加快此过程,我们希望不可行的枚举尽快被否定,因此我们
希望首先使用$k$较小并且$X$较大的条件判定,因为这个条件是更强的,于是考虑按照$\frac{k}{X}$从小到大的顺序检验答案$i$是否满足当前
条件。由此问题便可解决。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <iostream>
#include <assert.h>
#define pi acos(-1.)
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << ) - );
const int mod = 1e9 + ;
const double double_inf = 1e30;
typedef unsigned long long ul;
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) cout << x << endl
#define dbg2(x, y) cout << x << " " << y << endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
#define low_bit(x) ((x) & (-x)) inline int readint(){
int x;
scanf("%d", &x);
return x;
} inline int readstr(char *s){
scanf("%s", s);
return strlen(s);
} class cmpt{
public:
bool operator () (const int &x, const int &y) const{
return x > y;
}
}; int Rand(int x, int o){
//if o set, return [1, x], else return [0, x - 1]
if(!x) return ;
int tem = (int)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
} void data_gen(){
srand(time());
freopen("in.txt", "w", stdout);
int times = ;
printf("%d\n", times);
while(times--){
int r = Rand(, ), a = Rand(, ), c = Rand(, );
int b = Rand(r, ), d = Rand(r, );
int m = Rand(, ), n = Rand(m, );
printf("%d %d %d %d %d %d %d\n", n, m, a, b, c, d, r);
}
} struct cmpx{
bool operator () (int x, int y) { return x > y; }
};
int debug = ;
int dx[] = {-, , , };
int dy[] = {, , -, };
//-------------------------------------------------------------------------
int C, S;
ll mt[][];
ll X[], k[];
ll ans[];
ll buf[];
ll w[], _w[];
ll M;
int id[];
pair<pll, int> _buf[];
const int lim = 1e5; void egcd(ll a, ll b, ll &d, ll &x, ll &y){
if(!b){
d = a, x = , y = ;
return;
}
egcd(b, a % b, d, x, y);
ll x1 = x, y1 = y;
x = y1, y = x1 - a / b * y1;
} void dfs(int pos){
if(pos == C + ){
ll tem = ;
FOR(i, , C) tem += buf[i] * w[i] % M * _w[i] % M, tem %= M;
if(!tem) tem = M;
FOR(i, , S - ) ans[i + S] = tem + M * i;
sort(ans, ans + * S);
return;
}
FOR(i, , k[pos]){
buf[pos] = mt[pos][i];
dfs( + pos);
}
} bool cmp(pair<pll, int> x, pair<pll, int> y){
return x.st.st * y.st.nd < x.st.nd * y.st.st;
} void enu_solve(){
FOR(i, , C) _buf[i] = mp(mp(k[i], X[i]), i);
sort(_buf + , _buf + C + );
FOR(i, , C) id[i] = _buf[i].nd;
int cnt = ;
int bg = ;
while(cnt < S){
int ok1 = ;
FOR(i, , C){
int j = id[i];
int tem = bg % X[j];
int sz = k[j];
int ok = ;
FOR(u, , sz) if(tem == mt[j][u]) { ok = ; break; }
if(!ok) { ok1 = ; break; }
}
if(ok1) ans[cnt++] = bg;
bg++;
}
} void crt_solve(){
M = ;
FOR(i, , C) M *= X[i];
FOR(i, , C) w[i] = M / X[i] % M;
FOR(i, , C){
ll d, x, y;
egcd(w[i], X[i], d, x, y);
_w[i] = (x % X[i] + X[i]) % X[i];
}
clr(ans, ll_inf);
dfs();
} void solve(){
ll num = ;
int ok = ;
FOR(i, , C){
num *= k[i];
if(num > lim){
ok = ;
break;
}
}
if(ok) crt_solve();
else enu_solve();
} //-------------------------------------------------------------------------
int main(){
//data_gen(); return 0;
//C(); return 0;
debug = ;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(debug) freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(~scanf("%d%d", &C, &S) && C){
FOR(i, , C){
X[i] = readint(), k[i] = readint();
FOR(j, , k[i]) mt[i][j] = readint();
}
solve();
FOR(i, , S - ) printf("%lld\n", ans[i]);
printf("\n");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
return ;
}
code:
Uva 11754 Code Feat的更多相关文章
- 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 (枚举,中国剩余定理)
转载请注明出处: 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 中国剩余定理+枚举
Code FeatUVA - 11754 题意:给出c个彼此互质的xi,对于每个xi,给出ki个yj,问前s个ans满足ans%xi的结果在yj中有出现过. 一看便是个中国剩余定理,但是同余方程组就有 ...
- UVA 11754 Code Feat 中国剩余定理+暴力
lrj白书例题,真好 #include <stdio.h> #include <iostream> #include <vector> #include <m ...
- UVA - 11754 Code Feat (分块+中国剩余定理)
对于一个正整数N,给出C组限制条件,每组限制条件为N%X[i]∈{Y1,Y2,Y3,...,Yk[i]},求满足条件的前S小的N. 这道题很容易想到用中国剩余定理,然后用求第k小集合的方法输出答案.但 ...
- UVA 11557 - Code Theft (KMP + HASH)
UVA 11557 - Code Theft 题目链接 题意:给定一些代码文本.然后在给定一个现有文本,找出这个现有文本和前面代码文本,反复连续行最多的这些文本 思路:把每一行hash成一个值.然后对 ...
- UVa 11754 (中国剩余定理 枚举) Code Feat
如果直接枚举的话,枚举量为k1 * k2 *...* kc 根据枚举量的不同,有两种解法. 枚举量不是太大的话,比如不超过1e4,可以枚举每个集合中的余数Yi,然后用中国剩余定理求解.解的个数不够S个 ...
- UVA11754 - Code Feat
Hooray! Agent Bauer has shot the terrorists, blown upthe bad guy base, saved the hostages, exposed ...
随机推荐
- 最长上升子序列的变形(N*log(N))hdu5256
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 面向切面编程AOP:基于XML文件的配置
除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的. 首先建立一个类: package com.sevenhu ...
- UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players A and B get ...
- swt byte[] 与 Image的转换
1. 从byte[]得到Image private static Image createImage(byte[] imageBytes) { Image image = null; try { By ...
- Android -- 初探MVP模式
1,相信大家对mvp模式都很熟悉了,M-Model-模型.V-View-视图.C-Controller-控制器.MVP作为MVC的版本演化,与MVC的意义类似:M-Model-模型.V-View-视图 ...
- paper 49:论文退稿?审稿人帮你总结了22个能避免的常见问题
很多投稿出去的文章都是可上可下的.往往退稿的时候,审稿人提了一堆意见,说退稿.但是大家想过没有?如果能事先预测到这些意见,或者请懂行的人事先看过文章预测出意见,然后根据这些意见修改好了再投出去,说不定 ...
- paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。
在最开始讨论支持向量机的时候,我们就假定,数据是线性可分的,亦即我们可以找到一个可行的超平面将数据完全分开.后来为了处理非线性数据,使用 Kernel 方法对原来的线性 SVM 进行了推广,使得非线性 ...
- 某个系统配置文件 用户层的SQL
SELECT fpo.user_profile_option_name, fpv.level_id, fpv.level_value, fpv.profile_option_value, fu.use ...
- 关于archlinux下的ralink5370网卡
驱动此网卡要使用 rt2800usb,rt2800lib 这两个模块 顺便说一下对模块进行操作的命令: rmmod 模块名 //为移除模块 insmod 模块所在路径 //为添加模块 查看网卡是否能被 ...
- Java应用程序的打包和发布
Java应用程序的打包和发布 简化Java应用程序的打包和发布 发布Java应用程序时Java提供了一系列打包和发布工具,可以显著的简化发布过程 该文章提供了打包Java code的几种方法,探讨Ja ...