UVALive 6912
来源:互联网 发布:光棍儿 知乎 编辑:程序博客网 时间:2024/06/04 00:54
题目描述:
超级好超级赞的一道题.一共有1000个灯.一共最多有1000个开关.每个开关都是一个质数会控制灯的编号是这个质数倍数的灯.保证每个灯的质数都不一样.问最多能让几个灯同时亮.
题解:
开灯问题目前还没遇到贪心的方法…这个题目也是. 质数是关键. 我们想如果开关很少,就可以二进制暴力枚举.但是开关很多. 那么有没有一些开关是不用暴力枚举的.那些大于sqrt(n)的开关和开关之间是没有影响的!!!英尺对于小于等于sqrt(n)以内的开关暴力枚举, 对于之后的开关,是没有后效性的.如果这个开关能够带来更多的灯,我们就开.
重点:
质数想sqrt.sqrt之后发现之后的开关没有影响. 然后就可以暴力枚举了.
代码:
#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>using namespace std;int a[1024], b[1024], n, k;int main() { int T; scanf("%d", &T); for (int cas = 1; cas <= T; ++cas) { scanf("%d%d", &n, &k); for (int i = 0; i < k; ++i) scanf("%d", &a[i]); sort(a, a + k); k = unique(a, a + k) - a; int limit = k; for (int i = 1; i < k; ++i) { if (a[i] > 35) { limit = i; break; } } int ret = 0; for (int sta = 0; sta < (1 << limit); ++sta) { int ans = 0; memset(b, 0, sizeof b); for (int i = 0; i < limit; ++i) { if ((1 << i) & sta) { for (int j = a[i]; j <= n; j += a[i]) { b[j] ^= 1; if (b[j]) { ans++; } else { ans--; } } } } for (int i = limit; i < k; ++i) { int tmp = 0; for (int j = a[i]; j <= n; j += a[i]) { if (b[j]) { tmp--; } else { tmp++; } } if (tmp > 0) ans += tmp; } if (ans > ret) ret = ans; } printf("Case #%d: %d\n", cas, ret); } return 0;}
0 0
- UVALive 6912
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- PHP根据经纬度计算两点间实际距离
- Android Service的简介(自用)
- PriorityQueue ,ArrayList , 数组排序
- java多线程-专题-聊聊并发(六)ConcurrentLinkedQueue的实现原理分析
- java多线程设计模式--Balking不需要就算了
- UVALive 6912
- android-percent-support-extend根据百分比高效自动适配页面库。
- 【数据结构|剑指Offer】单向链表的各项操作实现
- jquery 深入学习笔记之一 (事件绑定)
- CodeForces 363C - Fixing Typos
- Mac 程序员必备软件
- 玩转HTML5移动页面(动效篇)
- Java Socket编程
- 【计蒜客系列】挑战难题23:计数和数数