UVALive 6912 Prime Switch 暴力枚举+贪心
题目链接:
Prime Switch
输出
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-----------------------------------------------------------------------