题目链接:

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

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 7

sample 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 暴力枚举+贪心的更多相关文章

  1. UVALive 6912 Prime Switch 状压DP

    Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

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

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

  3. 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 ...

  4. Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心

    2241: [SDOI2011]打地鼠 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1022  Solved: 651[Submit][Status ...

  5. P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)

    技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...

  6. 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 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  8. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  9. Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)

    题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...

随机推荐

  1. Spring Bean d的作用域

    在spring中,可以在<bean>元素的scop属性里设置bean的作用域,以决定这个bean是单实例的还是多实例的. 默认情况下,spring只为每个在IOC容器里声明的bean创建唯 ...

  2. BootStrapValidate 简单使用

    前阵子用了bootstrapvalidate写了一个登录验证,这里小记一笔 首先需要引入 bootstrapValidator.css //可不引入 jquery-2.1.0.min.js boots ...

  3. 001_02-python基础习题答案

    python 基础习题 执行 Python 脚本的两种方式 如:脚本/python/test.py 第一种方式:python /python/test.py 第二中方式:在test.py中声明:/us ...

  4. 新增时json类型报错

    新增时出错:如下 实体类中字段类型没有对应上,vue页面中修改跳转页面的路径:使用params...

  5. 话说文件系统——aufs源码分析(三)

    1. linux中设备驱动的入口都是:module_init(xxx_init);里面注册的函数,对于文件系统来说也是一样的,对于aufs来说,就是aufs_init,具体如下 //用于描述aufs文 ...

  6. 自学tensorflow——1.框架初步了解以及构建简单的计算图计算

    1.初步了解 tensorflow是谷歌的一款开源深度学习框架.运行前,需要先定义好计算图,最后通过会话启动计算图,这么做的目的是为了防止数据在python和c++(tensorflow底层)传输的时 ...

  7. kubernetes命令式容器应用编排/部署应用/探查应用详情/部署service对象/扩缩容/修改删除对象

    部署Pod应用 创建delpoyment控制器对象 [root@master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --port=80 ...

  8. HTTP請求

    HTTP[超文本傳輸協議]是因特網上應用最為廣泛的一種網絡傳輸協議,送油的WWW文件都必須遵守這個標準. HTTP是一個基於TCP/IP通信協議來產地數據(html文件,圖片文件,查詢結果等). HT ...

  9. FPGA静态时序分析基础

    FPGA静态时序分析基础 基本概念 Skew: 时钟偏移 Skew表示时钟到达不同触发器的延时差别,Tskew = 时钟到达2号触发器的时刻 - 时钟到达1号触发器的时刻. Jitter: 时钟抖动 ...

  10. 20155310 2016-2017-2 《Java程序设计》第十周学习总结

    20155310 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 •网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就 ...