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();
}

转载于:https://www.cnblogs.com/qscqesze/p/5954466.html

Xtreme9.0 - Light Gremlins 容斥相关推荐

  1. Leetcode 552.学生出勤记录‖ 动态规划+容斥

    题目链接:传送门 可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤.迟到.到场).记录中只含下面三种字符: 'A':Absent,缺勤 'L':Late,迟到 'P':P ...

  2. HDU 6143 Killer Names(排列+容斥,dp)

    Killer Names HDU 6143 (容斥+排列组合,dp+整数快速幂) 2017ACM暑期多校联合训练 - Team 8 1011 Killer Names 题目链接 Time Limit: ...

  3. 组合-二项式定理及容斥

    请准备好清醒的大脑接受公式的轰炸! 二项式定理: $(1+x)^n=\sum_{i=0}^n  \lgroup \begin{array}{} n \\ i \end{array} \rgroup  ...

  4. 之江学院第0届 A qwb与支教 容斥与二分

    题目链接: http://115.231.222.240:8081/JudgeOnline/problem.php?cid=1005&pid=0 题目描述: 给你三个数x, y, z 和 N ...

  5. 容斥 + 树形dp ---- 2021 icpc 沈阳 L Perfect Matchings

    题目链接 题目大意: 就是给你一个2n2n2n个点的完全图,从这个图里面抽出2n−12n-12n−1条边,这些边形成一颗树,现在问你剩下的图里面点进行完美匹配有多少种方案? 解题思路: 一开始被完美匹 ...

  6. Luogu P4336 [SHOI2016]黑暗前的幻想乡(容斥,矩阵树定理,子集反演)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Luogu P4336 [SHOI2016]黑暗前的幻想乡(容斥,矩阵树定理) Problem n≤1 ...

  7. P3175 [HAOI2015]按位或(Min - Max容斥,FMT,概率期望,全网最清晰的题解!)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Weblink https://www.luogu.com.cn/problem/P3175 Prob ...

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. hdu 1796 How many integers can you find 容斥定理

    一开始看 这里 这个文章博主写得很好. 当举容斥定理的所谓 奇数为负 偶数为正的时候. 我直接就认为是 a*b 了.实际上是lcm(a,b). 由于博文中的因子都是互素的(素数之间).所以lcm(a, ...

最新文章

  1. linux 恢复win7引导,双系统重装win7后恢复ubuntu引导
  2. Docker怎样提交镜像(把容器打成镜像)
  3. https://www.usb.org/
  4. mysql恢复某张表的数据_mysql恢复某张表
  5. Fingersmith---指匠情挑
  6. java编程50题和解释_最新JAVA编程题全集(50题及答案)29515
  7. 文件夹1KB快捷方式(暴风一号)病毒的解决办法
  8. 算法:查找序列最小k元素(分治法)
  9. Boxx:一个旨在提高 Python 代码开发和调试效率的工具库,尤其在计算机视觉领域...
  10. RFID定位技术在智能出入库中的应用--新导智能
  11. 用npm安装yarn
  12. win10的服务器管理器在哪打开?
  13. kettle 6.1.0.1 mysql_kettle连接数据库报错:Error occured while trying to connect
  14. 小程序审核失败:你的小程序涉及提供播放、观看等服务,请补充选择:文娱-其他视频类目。怎么解决呢
  15. ROM、RAM、DRAM、SRAM、FLASH区别
  16. Hadoop HA (三) --------- HDFS-HA 自动模式
  17. Java中将String转成Long或long
  18. 导出远程oracle数据库到本地【亲测有效】
  19. python连接hive步骤(不出错版)
  20. 一款令人印象深刻的FCPX插件:3D Photo Animator (3D照片动画制作器)

热门文章

  1. java的exe生成工具(exe4j)
  2. pd4ml中文乱码解决
  3. 101、Spark Streaming之数据接收原理剖析与源码分析
  4. 天猫精灵淘公仔、丰厚奖品代金券!只要敢推荐,我们就敢送!
  5. Spring Batch之读数据—读分隔符文件(二十六)
  6. 初中计算机flash考试题,【信息技术中考专区】Flash操作题专练(七)!
  7. MP3/MP4产业的魅族模式和纽曼模式
  8. 2022年全球市场单相交流变频器总体规模、主要生产商、主要地区、产品和应用细分研究报告
  9. 石油地质数字孪生概述
  10. Google Earth Engine