Prime Switch

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4924

Description

There are lamps (uniquely numbered from 1 to N) and K switches. Each switch has one prime number

written on it and it is connected to all lamps whose number is a multiple of that prime number. Pressing

a switch will toggle the condition of all lamps which are connected to the pressed switch; if the lamp

is off then it will be on, and vice versa. You can press only one switch at one time; in other words,

no two switches can be pressed together at the same time. If you want to press multiple switches, you

should do it one by one, i.e. allowing the affected lamps of the previous switch toggle their condition

first before pressing another switch.

Initially all the lamps are off. Your task is to determine the maximum number of lamps which can

be turned on by pressing one or more switches.

For example, let there be 10 lamps (1 . . . 10) and 2 switches which numbers are 2 and 5 as shown

in the following figure.

In this example:

• Pressing switch 2 will turn on 5 lamps: 2, 4, 6, 8, and 10.

• Pressing switch 5 will turn on 2 lamps: 5 and 10.

• Pressing switch 2 and 5 will turn on 5 lamps: 2, 4, 5, 6, and 8. Note that lamp number 10 will

be turned off as it is toggled twice, by switch 2 and switch 5 (off → on → off).

Among all possible switches combinations, the maximum number of lamps which can be turned on

in this example is 5.

Input

The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case begins

with two integers in a line: N and K (1 ≤ K ≤ N ≤ 1, 000), denoting the number of lamps and

switches respectively. The next line contains K distinct prime numbers, each separated by a single

space, representing the switches number. You are guaranteed that the largest number among those

switches is no larger than N

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the maximum

number of lamps which can be turned on for that particular case.

Explanation for 2nd sample case:

You should press switch 2 and 7, such that 11 lamps will be turned on: 2, 4, 6, 7, 8, 10, 12, 16, 18,

20, and 21. There exist some other combinations which can turn on 11 lamps, but none can turn more

than 11 lamps on.

Explanation for 3rd sample case:

There is only one switch, and pressing it will turn 20 lamps on.

Explanation for 4th sample case:

Pressing all switches will turn 42 lamps on, and it is the maximum possible in this case

Sample Input

4

10 2

2 5

21 4

2 3 5 7

100 1

5

100 3

3 19 7

Sample Output

Case #1: 5

Case #2: 11

Case #3: 20

Case #4: 42

Hint

题意

你有n盏灯,有m个开关,开关上面都写着一个质数

那么这个开关就控制着上面写着的数字的倍数。

灯泡按奇数次就亮着,偶数次,就熄灭。

问你最好情况下,最优有多少个灯亮着。

题解:

对于小于等于31的素数,我们状压去跑dp,对于大于的,我们就贪心。

因为大于31的素数一定是不会冲突的,因为上面的数乘起来就大于1000了。

然后这样就行了。

代码

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1000 + 15;
int N,K,pr[maxn],pre[maxn],prime[maxn],primelen,ha[maxn],tot,op[maxn],flag[maxn],temp[maxn];
vector < int > ap; void Init(){
memset( ha , -1 , sizeof( ha ) );
for(int i = 2 ; i < maxn ; ++ i) if(!pre[i]){
for(int j = i + i ; j < maxn ; j += i) pre[j] = 1;
prime[ primelen ++ ] = i;
}
} int solve( int bit ){
for(int i = 1 ; i <= N ; ++ i) flag[i] = 0;
for(int i = 0 ; i < tot ; ++ i) if( bit >> i & 1 ){
for(int j = op[i] ; j <= N ; j += op[i] ) flag[j] ^= 1;
}
for(auto it : ap){
int add = 0;
for(int i = it ; i <= N ; i += it) if( flag[i] == 0 ) ++ add ; else -- add;
if( add > 0 ) for(int i = it ; i <= N ; i += it) flag[i] ^= 1;
}
int rs = 0;
for(int i = 1 ; i <= N ; ++ i) rs += flag[i];
return rs;
} int main(int argc,char *argv[]){
int T,cas=0;
Init();
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&K);
for(int i = 0 ; i < K ; ++ i) scanf("%d" , pr + i);
sort( pr , pr + K );
tot = 0;ap.clear();
for(int i = 0 ; i < K ; ++ i) if( pr[i] <= 31 ) op[tot ++ ] = pr[i];else ap.push_back( pr[i] );
int mx = 0;
for(int i = 0 ; i < (1 << tot) ; ++ i) mx = max( mx , solve( i ) );
printf("Case #%d: %d\n", ++ cas , mx);
}
return 0;
}

UVALive 6912 Prime Switch 状压DP的更多相关文章

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

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

  2. UVALive - 6912 Prime Switch (状压DP)

    题目链接:传送门 [题意]有n个灯,m个开关,灯的编号从1~n,每个开关上有一个质数,这个开关同时控制编号为这个质数的倍数的灯,问最多有多少灯打开. [分析]发现小于根号1000的质数有10个左右,然 ...

  3. UVALive 6912 Prime Switch 暴力枚举+贪心

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  4. 状压DP uvalive 6560

    // 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...

  5. UVAlive 6560 - The Urge to Merge(状压dp)

    LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...

  6. LGTB与序列 状压dp

    考试一看我就想到了状压dp.当时没有想到素数,以为每一位只有0~9这些数,就开始压了.后来发现是小于30,然后改到了15,发现数据一点不给面子,一个小点得数都没有,完美爆零.. 考虑到bi最多变成58 ...

  7. Codeforces 895C - Square Subsets 状压DP

    题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...

  8. 洛谷P2761 软件补丁问题(状压dp)

    传送门 啊咧……这题不是网络流二十四题么……为啥是个状压dp…… 把每一个漏洞看成一个状态,直接硬上状压dp 然后因为有后效型,得用spfa //minamoto #include<iostre ...

  9. HDU-3681-Prison Break(BFS+状压DP+二分)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

随机推荐

  1. hdu 5385 The path

    http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意: 给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得 ...

  2. Mac下安装zsh(Oh My ZSH)的shell,替代原有的bash

    说明:一开始装zsh我是拒绝的,因为这个东西装简单,卸载很难,并且装了之后默认Shell的配置文件不能用了,比如~/.bashrc这些.所以在装的时候要再三考虑好! 官网:http://ohmyz.s ...

  3. Linux 基础知识(一) shell的&&和|| 简单使用

    shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? == 1 时,表示执行失败.  有时候,下一条命令依赖前 ...

  4. 流媒体技术学习笔记之(七)进阶教程OBS参数与清晰度流畅度的关系

    源码地址:https://github.com/Tinywan/PHP_Experience 很多主播问过OBS的参数到底什么影响画质,到底什么影响流畅度,那么本篇教程尽量用通俗的语言解释下一些重要参 ...

  5. 一个很实用的css3兼容工具很多属性可以兼容到IE6

    当你看到这样的效果图是不是已经崩溃了 css3没出来之前大部分人基本都是用图片的方式拼出来的 腾讯邮箱就是这么做的 然后你想和设计说换直角吧.我用图片的好烦的感觉!而且我们还要兼容到ie6 她和你说别 ...

  6. CyberArticle(eLib电子图书馆)网文快捕

    CyberArticle (网文快捕)是一款知识管理软件,主要致力于网页的保存和后期管理.CyberArticle (网文快捕)主要功能,就是收集和整理网页.利用CyberArticle (网文快捕) ...

  7. Project Euler Problem4

    Largest palindrome product Problem 4 A palindromic number reads the same both ways. The largest pali ...

  8. 一个无锁消息队列引发的血案(四)——月:RingQueue(上) 自旋锁

    目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...

  9. 如何动态修改windows下的host文件

    事件背景:为了测试数据提交后,需要在另一个环境的多个测试节点下去验证测试数据是否添加成功,找了一大堆放法,用了比较笨的方法实现了.不多废话思路如下: 为了万无一失,先备份hosts文件内容: 1.读取 ...

  10. PHPWeb开发相关知识

    转载:https://blog.csdn.net/wj610671226/article/details/78426698 正则表达式 作用:分割.查找.匹配.替换字符串 分割符:正斜线(/).has ...