UVALive - 6912

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.
Input
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.
Output
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

分一部分枚举所有状态,然后看剩下的是否对答案有贡献;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
#define lson 2*x
#define rson 2*x+1

long long  qupower(ll a, ll b,ll mod) {
    long long  ans = 1;
    while (b) {
        if (b & 1)ans = ans * a%mod;
        b >>= 1;
        a = a * a%mod;
    }
    return ans;
}

inline int read() {
    int an = 0, x = 1; char c = getchar();
    while (c > '9' || c < '0') {
        if (c == '-') {
            x = -1; 
        }
        c = getchar();
    }
    while (c >= '0'&&c <= '9') {
        an = an * 10 + c - '0'; c = getchar();
    }
    return an * x;
}


int vis[maxn];

int t, n, k;


void Find(int &res, int x) {
    int i, j;
    for (i = x; i <= n; i += x) {
        if (vis[i] == 1)vis[i] = 0;
        else vis[i] = 1;
        if (vis[i])res++;
        else res--;
    }
}

int main() {
    //ios::sync_with_stdio(false);

    //cin >> t;
    //t = read();
    scanf("%d", &t);
    int cnt = 0;

    while (t--) {
        cnt++;
        int i, j;
        //ms(vis);
        //ve1.clear(); ve2.clear();

        vector<int>ve1, ve2;
        //cin >> n >> k;
        //n = read(); k = read();
        scanf("%d%d", &n, &k);

        for (i = 1; i <= k; i++) {
            //cin >> a[i];
            //a[i] = read();
            int c; 
            c = read();
            //scanf("%d", &c);
            if (c  < 29)ve1.push_back(c);
            else ve2.push_back(c);
        }

        //cout << "Case #" << cnt << ": ";
        //printf("Case #%d: ", cnt);
        int maxx = 0;
        for (i = 0; i < (1 << ve1.size()); i++) {
            int res = 0;
            ms(vis);

            for (j = 0; j < ve1.size(); j++) {

                if ((1 << j)&i) {
                    Find(res, ve1[j]);
                }
            }

            for (j = 0; j < ve2.size(); j++) {
                int tmp = 0;
                Find(tmp, ve2[j]);
                res += max(0, tmp);
            }

            maxx = max(maxx, res);
        }
        //cout << maxx  << endl;
        printf("Case #%d: %d\n", cnt, maxx);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/81660585
今日推荐