zoukankan      html  css  js  c++  java
  • Xtreme9.0

    Xtreme9.0 - Light Gremlins

    题目连接:

    https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/light-gremlins

    Description

    There are a group of gremlins that live in a long hallway in which there are a series of light switches. At the beginning of each night, all of the light switches are off. Then, one at a time, each gremlin does the following:

    The gremlin chooses a prime number p, that has not been chosen by any previous gremlin that night.

    The gremlin runs down the hallway flipping every pth switch.

    The owner of the hallway, who is very concerned about his electricity bill, has asked you to determine how many switches are on at the end of the night.

    Note: no two gremlins will choose the same prime number.

    Consider the following example where the hallway has 21 switches and there are three gremlins. At the beginning of the night, all switches are off, as shown in the figure below.

    state1.png

    The first gremlin chooses the prime number 7, and flips the 7th, 14th, and 21st switch. Now the configuration is:

    state2.png

    The second gremlin chooses the prime number 13, and flips just the 13th switch, because there is no 26th switch. Now the configuration is:

    state3.png

    The last gremlin chooses the prime number 3. It flips the 3rd, 6th, 9th, 12th, 15th, 18th, and 21st switch. Note that when he flips the 21st switch, it is turned back off. The final configuration is:

    state4.png

    For this example, you would report that there are 9 lights on at the end of the night.

    Input

    The input begins with an integer t, 1 <= t <= 20, on a line by itself.

    Then follow t lines, each describing a test case that you must evaluate. The test cases have the following format:

    [switch] [n] [prime_1] [prime_2] ... [prime_n]
    Where

    [switch] is the number of switches in the hallway, 1 <= [switch] <= 1018

    [n] is the number of gremlins who live in the hallway, 1 <= [n] <= 24

    The prime number chosen by the ith gremlin is given by [prime_i]. All primes are greater than or equal to 2 and less than 104.

    Output

    For each test case, you should output a single integer that indicates how many switches are on at the end of the night.

    Sample Input

    3
    21 3 7 13 3
    20 1 31
    30 3 2 3 5

    Sample Output

    9
    0
    15

    Hint

    The first test case corresponds to the example given in the Problem Definition, which as described above results in 9 "on" switches at the end of the night.

    In the second test case, there is a single gremlin, who chooses the prime 31. The hallway consists of only 20 switches, so there is no 31st switch. Thus, no switches are turned on.

    The last test case consists of a hallway of length 30, and three gremlins. The action of the gremlins is as follows:

    The first gremlin flips switches {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30}. All of these switches were previously off, so they are now on.

    The second gremlin flips switches {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}. Of these, {6, 12, 18, 24, 30} were previously on, so they are now off. This results in the following switches being on: {2, 3, 4, 8, 9, 10, 14, 15, 16, 20, 21, 22, 26, 27, 28}.

    The third gremlin flips switches {5, 10, 15, 20, 25, 30}. Of these, {10, 15, 20} were previously on, so they are now off. This results in the following switches being on: {2, 3, 4, 5, 8, 9, 14, 16, 21, 22, 25, 26, 27, 28, 30}.

    Thus, there are 15 switches on at the end of the night.

    题意

    有n个灯泡,然后给你m个操作,每次操作都是把能够模p[i]等于0的灯泡改变状态,一开始都是灭的。

    问你最后有多少个灯泡亮着。

    题解

    傻逼容斥。

    代码

    #include<bits/stdc++.h>
    #include<stdio.h>
    using namespace std;
    
    long long ans = 0;
    long long p[25];
    long long n;
    long long two[26];
    int num;
    void dfs(long long x,long long pp,int flag,int num1)
    {
        if(flag)ans+=n/pp*two[num1];
        else ans-=n/pp*two[num1];
        for(int i=x+1;i<num;i++)
        {
            if(1.0*pp*p[i]>1.0*n)continue;
            dfs(i,pp*p[i],1-flag,num1+1);
        }
    }
    void solve()
    {
        two[1]=1;
        for(int i=2;i<26;i++)
            two[i]=two[i-1]*2LL;
        ans=0;
        cin>>n>>num;
        for(int i=0;i<num;i++)
            scanf("%lld",&p[i]);
        for(int i=0;i<num;i++)
            dfs(i,p[i],1,1);
        cout<<ans<<endl;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    实验1.2 C语言上机入门 二
    如何使用OJ系统
    (第五周)工作总结
    (第五周)团队项目2
    (第五周)团队项目1
    (第五周)立项申请更新(食物链教学工具)
    (第四周)工作总结
    (第四周)四则运算单元测试
    (第四周)词频统计单元测试
    (第三周)工作总结
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5954466.html
Copyright © 2011-2022 走看看