UVALive 6912 Prime Switch 暴力枚举+贪心
题目链接:
Prime Switch
Time limit: 1.000 seconds
#### 问题描述
> 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.
#### 输入
> 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.
输出
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 7sample output
Case #1: 5
Case #2: 11
Case #3: 20
Case #4: 42
题意
给你n盏灯,你有k个质数开关,每个质数开关可以控制是它的倍数的灯,每盏灯被打开奇数次才会亮,问如何控制开关使得亮的灯泡最多
题解
对于<31的质数开关,直接暴力枚举所有状态,对于>=31的开关(任意两个>=31的质数开关不可能共同控制同一盏灯),就贪心一下,如果把它开起来能激活更多的灯,就开,否则就关。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1111;
int n,m;
int light[maxn];
void solve(int &ret,int x){
for(int i=x;i<=n;i+=x){
light[i]^=1;
if(light[i]) ret++;
else ret--;
}
}
int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d%d",&n,&m);
VI a1,a2;
rep(i,0,m){
int x; scf("%d",&x);
if(x<31) a1.pb(x);
else a2.pb(x);
}
int ans=0;
rep(i,0,(1<<a1.sz())){
clr(light,0);
int cnt=0;
rep(j,0,a1.sz()){
if((1<<j)&i){
solve(cnt,a1[j]);
}
}
rep(j,0,a2.sz()){
int tmp=0;
solve(tmp,a2[j]);
cnt+=max(0,tmp);
}
ans=max(ans,cnt);
}
prf("Case #%d: %d\n",++kase,ans);
}
return 0;
}
//end-----------------------------------------------------------------------
UVALive 6912 Prime Switch 暴力枚举+贪心的更多相关文章
- UVALive 6912 Prime Switch 状压DP
Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- UVALive - 6912 Prime Switch (状压DP)
题目链接:传送门 [题意]有n个灯,m个开关,灯的编号从1~n,每个开关上有一个质数,这个开关同时控制编号为这个质数的倍数的灯,问最多有多少灯打开. [分析]发现小于根号1000的质数有10个左右,然 ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心
2241: [SDOI2011]打地鼠 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1022 Solved: 651[Submit][Status ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)
技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...
- Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- bzoj 1028 暴力枚举判断
昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...
- Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)
题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...
随机推荐
- php ajax confirm 删除
<button name="del" type="button" class="btn btn-primary btn-xs" id= ...
- mkdir 的详细使用说明
mkdir 是make directory [dɪˈrɛktəri, daɪ-]的缩写,directory--目录的意思 mkdir在linux中是指新建文件目录 例如:mkdir test3 如果要 ...
- 基于STM32F103ZET6 HC_SR501人体红外感应
这是最后的实验现象,有人走过会一直输出有人,离开范围时则输出没人 开发板 PZ6086L ,HC_SR501模块 这是HC_SR501的示意图,,VCC和GND不再多做介绍,5V供电就行, OUT接口 ...
- vue 解决跨域问题
1.后端处理允许跨域 2.反向代理跨域 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去去的网络信息.形象的说:它是网络信息的中转站. vue中设置代理: 1.config/in ...
- 20155220 2016-2017-2 《Java程序设计》第10周学习总结
20155220 2016-2017-2 <Java程序设计>第10周学习总结 教材内容学习总结 计算机网络编程概述 路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控 ...
- 罗佳琪的第三次预备作业——虚拟机的安装及Linux的初步学习
虚拟机的安装及Linux的初步学习 坎坷的安装过程 首先我按照老师给的基于VirtualBox虚拟机安装Ubuntu图文教程进行了下载,下载很顺利但是安装时出现了问题. 起初我以为是电脑位数问题,但我 ...
- 【转载】OLE控件在Direct3D中的渲染方法
原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么 ...
- 【linux报错】-bash: xhost: command not found
参考自:http://blog.csdn.net/csdnones/article/details/51513163,感谢原作者解决了我的问题. 执行xhost +,报以下错误,原因是因未没有安装相关 ...
- dubbo常见的一些面试题
什么是Dubbo? Duubbo是一个RPC远程调用框架, 分布式服务治理框架 什么是Dubbo服务治理? 服务与服务之间会有很多个Url.依赖关系.负载均衡.容错.自动注册服务. Dubbo有哪些协 ...
- 函数parseQuery用于解析url查询参数
在百度上找的,以后忘了再看. 语法如下: var obj = parseQuery(query) query是被解析的查询参数,函数返回解析后的对象. 使用范例如下: var jerry = pars ...