题目链接:

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的质数开关不可能共同控制同一盏灯),就贪心一下,如果把它开起来能激活更多的灯,就开,否则就关。

代码

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. using namespace std;
  17. #define X first
  18. #define Y second
  19. #define mkp make_pair
  20. #define lson (o<<1)
  21. #define rson ((o<<1)|1)
  22. #define mid (l+(r-l)/2)
  23. #define sz() size()
  24. #define pb(v) push_back(v)
  25. #define all(o) (o).begin(),(o).end()
  26. #define clr(a,v) memset(a,v,sizeof(a))
  27. #define bug(a) cout<<#a<<" = "<<a<<endl
  28. #define rep(i,a,b) for(int i=a;i<(b);i++)
  29. #define scf scanf
  30. #define prf printf
  31. typedef long long LL;
  32. typedef vector<int> VI;
  33. typedef pair<int,int> PII;
  34. typedef vector<pair<int,int> > VPII;
  35. const int INF=0x3f3f3f3f;
  36. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  37. const double eps=1e-8;
  38. const double PI = acos(-1.0);
  39. //start----------------------------------------------------------------------
  40. const int maxn=1111;
  41. int n,m;
  42. int light[maxn];
  43. void solve(int &ret,int x){
  44. for(int i=x;i<=n;i+=x){
  45. light[i]^=1;
  46. if(light[i]) ret++;
  47. else ret--;
  48. }
  49. }
  50. int main() {
  51. int tc,kase=0;
  52. scf("%d",&tc);
  53. while(tc--){
  54. scf("%d%d",&n,&m);
  55. VI a1,a2;
  56. rep(i,0,m){
  57. int x; scf("%d",&x);
  58. if(x<31) a1.pb(x);
  59. else a2.pb(x);
  60. }
  61. int ans=0;
  62. rep(i,0,(1<<a1.sz())){
  63. clr(light,0);
  64. int cnt=0;
  65. rep(j,0,a1.sz()){
  66. if((1<<j)&i){
  67. solve(cnt,a1[j]);
  68. }
  69. }
  70. rep(j,0,a2.sz()){
  71. int tmp=0;
  72. solve(tmp,a2[j]);
  73. cnt+=max(0,tmp);
  74. }
  75. ans=max(ans,cnt);
  76. }
  77. prf("Case #%d: %d\n",++kase,ans);
  78. }
  79. return 0;
  80. }
  81. //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. Jquery中select使用

    select获取当前选中的value $('#DDLDEP').change(function () { var depId = $(this).children('option:selected') ...

  2. STM32 HAL库学习系列第3篇 常使用的几种延时方式

    1   自带的hal_delay 函数    毫秒级延迟 void HAL_Delay(__IO uint32_t Delay) { uint32_t tickstart = HAL_GetTick( ...

  3. 海思平台交叉编译curl支持SSL功能

    1.准备工具 1).交叉编译工具 2).下载libcurl和openssl源代码,我使用的是(openssl-1.0.2o.tar,curl-7.59.0.tar) 3).查看cpu详细 ~ # ca ...

  4. PTA(BasicLevel)-1009 说反话

    一 .问题描述       原题描述 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出.不同于字符串逆序,这里需要的是将字符串中的单词顺序翻转. 输入样例: Hello World Here ...

  5. Go语言 异常panic和恢复recover用法

    Go语言 异常panic和恢复recover用法 背景:Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在 ...

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

    20155215 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络概论 - 网络是能够彼此通信的计算机的总和. - 网络分为局域网和广域网. - 按照计 ...

  7. 20155311 实验三 敏捷开发与XP实践 实验报告

    20155311 实验三 敏捷开发与XP实践 实验报告 实验内容 XP基础 xp核心工具 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...

  8. [agc006D]Median Pyramid Hard-[二分+乱搞]

    Description 题目大意:给你一个长度为n*2-1的排列,将除了该序列头尾的两个数外的其他数(设为i)变为原序列i-1,i,i+1下标数的中位数.求最后的数是什么.例子如下: Solution ...

  9. 【BZOJ3110】【LG3332】[ZJOI2013]K大数查询

    [BZOJ3110][LG3332][ZJOI2013]K大数查询 题面 洛谷 BZOJ 题解 和普通的整体分治差不多 用线段树维护一下每个查询区间内大于每次二分的值\(mid\)的值即可 然后再按套 ...

  10. cogs2554 [福利]可持久化线段树

    cogs2554 [福利]可持久化线段树 原题链接 每次修改复制一遍就行了... 1A!!! // It is made by XZZ #include<cstdio> #include& ...