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. Fibonacci数列(找规律)

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  2. java 获取路径

    1.利用System.getProperty()函数获取当前路径:System.out.println(System.getProperty("user.dir"));//user ...

  3. 如何用redis做到限制,一个手机号,1分钟内最多发一条,一天内最多10条

    需要两个缓存 key名称 phone-busy,缓存1分钟 key名称 phone-send-count,缓存1天,每成功发送一条+1 发送的时候流程如下: 判断phone-busy是否存在,存在直接 ...

  4. amazon lightsail

    https://51.ruyo.net/6038.html https://aws.amazon.com/cn/lightsail/

  5. HDU1569 方格取数(2) —— 二分图点带权最大独立集、最小割最大流

    题目链接:https://vjudge.net/problem/HDU-1569 方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory L ...

  6. NavigationView更改菜单icon和title颜色变化效果

    NavigationView menu默认icon和title会随着菜单状态改变而改变,选择某个菜单后再次打开侧边菜单后会发现该菜单的icon和title会变成应用的主颜色,其他菜单项仍然为黑色. 如 ...

  7. lucene 5的测试程序——API变动太大

    package hello; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import ...

  8. python中的异常处理try/except/finally/raise

    异常发生在程序执行的过程中,如果python无法正常处理程序就会发生异常,导致整个程序终止执行,python中使用try/except语句可以捕获异常. try/except 异常的种类有很多,在不确 ...

  9. Android studio Unable to run mksdcard SDK tool

    /******************************************************************************************** * Andr ...

  10. Code:NLog

    ylbtech-Code:NLog 1. NLog介绍使用返回顶部 1. NLog是什么 NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码.NLog ...