Coco dfs 或者 状压dp。...
Time Limit:1s Memory Limit:64MByte
Submissions:148Solved:85
Coco just learned a math operation call mod.Now,there is an integer aa and nn integers b1,…,bnb1,…,bn. After selecting some numbers from b1,…,bnb1,…,bn in any order, say c1,…,crc1,…,cr, Coco want to make sure that amodc1modc2mod…modcr=0amodc1modc2mod…modcr=0\ (i.e., aa will become the remainder divided by cici each time, and at the end, Coco want aa to become 00). Please determine the minimum value of rr. If the goal cannot be achieved, print −1−1 instead.
先对数组排序,从大到小,因为mod大的数,不会对MOD小的数有影响,但是因为余数比MOD小的数更大,所以因子数只会更多。机会只会更多,就像样例一样,先MOD 7,再MOD 2.
所以用dfs枚举每一个数,MOD和不MOD都选一次。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
int a[maxn];
int n, ans;
int val;
void dfs(int selct, int re, int cur) {
if (selct >= ans) return;
if (re == ) {
ans = min(ans, selct);
return;
}
if (cur > n) return;
dfs(selct + , re % a[cur], cur + );
dfs(selct, re, cur + );
}
void work() {
scanf("%d%d", &n, &val);
for (int i = ; i <= n; ++i) scanf("%d", &a[i]);
sort(a + , a + + n, greater<int>());
ans = inf;
dfs(, val, );
if (ans == inf) printf("-1\n");
else printf("%d\n", ans);
} int main() {
freopen("data.txt","r",stdin);
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
状压:dp[state]表示现在MOD了那位数字,然后枚举的时候,也是优先MOD了大的数字,再转移去小的数字。不允许先MOD小的再转移去MOD大的,就是2--3这条路径是不行的,因为2--3就是先MOD2,再MOD1得到,但是可以先MOD1,再MOD2得到。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e6 + ;
int dp[maxn];
int a[maxn];
int get(int val) {
int ans = ;
while (val) {
val &= (val - );
ans++;
}
return ans;
}
int cnt[maxn];
void init() {
int end = ( << ) - ;
for (int i = ; i <= end; ++i) {
cnt[i] = get(i);
}
return;
}
void work() {
int ans = inf;
int n, val;
scanf("%d%d", &n, &val);
for (int i = ; i < n; ++i) {
scanf("%d", &a[i]);
}
sort(a, a + n, greater<int>());
memset(dp, 0x3f, sizeof dp);
for (int i = ; i < n; ++i) {
dp[ << i] = val % a[i];
if (dp[ << i] == ) ans = ;
}
int end = ( << n) - ;
for (int j = ; j <= end; ++j) {
if (cnt[j] >= ans) continue;
for (int i = ; i < n; ++i) {
if ((( << i) & j) != ) continue;
int state = j | ( << i);
if (cnt[state] >= ans) continue;
if (dp[state] != inf) continue;
dp[state] = dp[j] % a[i];
if (dp[state] == ) ans = cnt[state];
}
}
if (ans == inf) printf("-1\n");
else printf("%d\n", ans);
} int main() {
init();
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
Coco dfs 或者 状压dp。...的更多相关文章
- POJ1038 Bugs Integrated, Inc 状压DP+优化
(1) 最简单的4^10*N的枚举(理论上20%) (2) 优化优化200^3*N的枚举(理论上至少50%) (3) Dfs优化状压dp O(我不知道,反正过不了,需要再优化)(理论上80%) (4) ...
- 【SCOI2005】互不侵犯 题解(状压DP)
前言:一道状压DP的入门题(可惜我是个DP蒟蒻QAQ) ------------------ 题意简述:求在一个$n*n$的棋盘中放$k$个国王的方案数.注:当在一个格子中放入国王后,以此格为中心的九 ...
- 【62测试】【状压dp】【dfs序】【线段树】
第一题: 给出一个长度不超过100只包含'B'和'R'的字符串,将其无限重复下去. 比如,BBRB则会形成 BBRBBBRBBBRB 现在给出一个区间[l,r]询问该区间内有多少个字符'B'(区间下标 ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...
- POJ 1321 棋盘问题(DFS & 状压DP)
用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...
- Calculation(dfs+状压dp)
Problem 1608 - Calculation Time Limit: 500MS Memory Limit: 65536KB Total Submit: 311 Accepted: ...
- HDU 4272 LianLianKan (状压DP+DFS)题解
思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...
- CODEVS1358【DFS/状压DP】
题目链接[http://codevs.cn/problem/1358/] 题意:这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每 ...
随机推荐
- 在Java中如何编写回调函数,以及回调函数的简单应用
import static java.lang.System.out; import static java.lang.System.err; import java.util.logging.Lev ...
- poj 1325 Machine Schedule 解题报告
题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 ...
- WAS:服务器停电后,重启DMGR,控制台访问不了
1. 今天有现场WAS服务器停电,重启DMGR后,控制台网页打不开: 后来得知,防火墙可能有问题.(虽然之前该机器上防火墙是关着的,但掉电后,防火墙会重启规则) 关掉防火墙后,问题解决. 2. ...
- DPI和PPI
写在前面 各种手机测频机构或者相关资讯老是谈及一个概念:ppi和dpi,通常总会忽略,只是稍微明白,这参数越高,说明屏幕分辨率越高:很长时间都止步如此:但作为一个iOS开发者,岂能止步如此,万一别人问 ...
- 我自己比较习惯的Watir自动化测试代码管理方式
- 【hyddd驱动开发学习】DDK与WDK
最近尝试去了解WINDOWS下的驱动开发,现在总结一下最近看到的资料. 1.首先,先从基础的东西说起,开发WINDOWS下的驱动程序,需要一个专门的开发包,如:开发JAVA程序,我们可能需要一个JDK ...
- cocos2d-js使用plist执行自身动作
首先需要将精灵动作帧动画图片使用TexturePacker创建plist,创建好后,将生成的plist和png图片(所有帧动画图片集成的一张大图): 百牛信息技术bainiu.ltd整理发布于博客园 ...
- 【旧文章搬运】修改PEB,断链隐藏模块成功
原文发表于百度空间,2008-7-26========================================================================== 继续实践之前 ...
- 编程 MD(d)、MT(d)编译选项的区别
转:http://blog.csdn.net/nodeathphoenix/article/details/7550546 1.各个选项代表的含义 编译选项 包含 静态链接的lib 说明 /MD _M ...
- vim的visual可视模式(转载)
转自:http://www.cnblogs.com/chenyadong/archive/2011/08/30/2159809.html 为了便于选取文本,VIM 引入了可视(Visual)模式.要选 ...