C -- Coco

Time Limit:1s Memory Limit:64MByte

Submissions:148Solved:85

DESCRIPTION

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.

INPUT
The first line contains one integer T(T≤5)T(T≤5), which represents the number of testcases. For each testcase, there are two lines: 1. The first line contains two integers nn and aa\ (1≤n≤20,1≤a≤1061≤n≤20,1≤a≤106). 2. The second line contains nn integers b1,…,bnb1,…,bn\ (∀1≤i≤n,1≤bi≤106∀1≤i≤n,1≤bi≤106).
OUTPUT
Print TT answers in TT lines.
SAMPLE INPUT
2 2 9 2 7 2 9 6 7
SAMPLE OUTPUT
2 -1

先对数组排序,从大到小,因为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。...的更多相关文章

  1. POJ1038 Bugs Integrated, Inc 状压DP+优化

    (1) 最简单的4^10*N的枚举(理论上20%) (2) 优化优化200^3*N的枚举(理论上至少50%) (3) Dfs优化状压dp O(我不知道,反正过不了,需要再优化)(理论上80%) (4) ...

  2. 【SCOI2005】互不侵犯 题解(状压DP)

    前言:一道状压DP的入门题(可惜我是个DP蒟蒻QAQ) ------------------ 题意简述:求在一个$n*n$的棋盘中放$k$个国王的方案数.注:当在一个格子中放入国王后,以此格为中心的九 ...

  3. 【62测试】【状压dp】【dfs序】【线段树】

    第一题: 给出一个长度不超过100只包含'B'和'R'的字符串,将其无限重复下去. 比如,BBRB则会形成 BBRBBBRBBBRB 现在给出一个区间[l,r]询问该区间内有多少个字符'B'(区间下标 ...

  4. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  5. UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)

    题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...

  6. POJ 1321 棋盘问题(DFS & 状压DP)

    用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...

  7. Calculation(dfs+状压dp)

    Problem 1608 - Calculation Time Limit: 500MS   Memory Limit: 65536KB    Total Submit: 311  Accepted: ...

  8. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  9. CODEVS1358【DFS/状压DP】

    题目链接[http://codevs.cn/problem/1358/] 题意:这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每 ...

随机推荐

  1. 链式mapreduce

    在hadoop 中一个Job中可以按顺序运行多个mapper对数据进行前期的处理,再进行reduce,经reduce后的结果可经个经多个按顺序执行的mapper进行后期的处理,这样的Job是不会保存中 ...

  2. bash shell parameter expansion

    1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...

  3. 百度新算法与网站SEO提升

  4. CH 5102 Mobile Service(线性DP)

    CH 5102 Mobile Service \(solution:\) 这道题很容易想到DP,因为题目里已经说了要按顺序完成这些请求.所以我们可以线性DP,但是这一题的状态不是很好设,因为数据范围有 ...

  5. Fastreport生成WEB报表

    开发WEB应用系统通常都会遇到报表打印问题.简单应用可利用IE的页面打印功能,利用HTML标签控制格式来实现.但复杂的业务型应用系统,报表不仅是组成应用的 重要部分,还常常是相当复杂的.现在很多应用系 ...

  6. php与html 表单的结合

    PHP $_POST <!DOCTYPE html> <html> <body> <form method="post" action=& ...

  7. 深入浅出Oracle学习笔记:Buffer Cache 和Shared pool

    Buffer cache 和 share pool 是sga中最重要最复杂的部分. 一.Buffer Cache 通常数据的读取.修改都是通过buffer cache 来完成的.buffer cach ...

  8. Go语言的管道Channel用法

    本文实例讲述了Go语言的管道Channel用法.分享给大家供大家参考.具体分析如下: channel 是有类型的管道,可以用 channel 操作符 <- 对其发送或者接收值. ch <- ...

  9. git push不成功 insufficient permission for adding an object to repository database

    这常见于多用户. 1. 确保所有用户在同一个组: 2. 确保所有文件被组可读写. 当多个用户各自进行了push操作后,object目录下的文件可能各自属于各个用户.

  10. LA-4356&&hdu-2469 (极角排序+扫描线)

    题目链接: Fire-Control System Time Limit: 12000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (J ...