Xtreme9.0 - Light Gremlins 容斥
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();
}
Xtreme9.0 - Light Gremlins 容斥的更多相关文章
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- How many integers can you find(容斥+dfs容斥)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...
- HDU 6397 组合数学+容斥 母函数
Character Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0
C - To Be an Dream Architect Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- 之江学院第0届校赛 qwb与支教 (容斥公式)
description qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平.她把小学生排成一排,从左至右从1开始依次往上报数. 玩完一轮后,他发现这个游戏太简单了.于是 ...
- 从 0 开始的min_max容斥证明
二项式反演 \[f_n=\sum\limits_{i=0}^nC^i_ng_i \Leftrightarrow g_n=\sum\limits_{i=0}^n{(-1)}^{n-i}f_i \] 证明 ...
- 【BZOJ-4455】小星星 容斥 + 树形DP
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 204 Solved: 137[Submit][Status] ...
- cf#305 Mike and Foam(容斥)
C. Mike and Foam time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- LaTeX文章结构
%导言 %\documentclass{article} %ctexbook \documentclass{ctexbook} \title{\heiti 监督学习} % 黑体 \author{\ka ...
- 第二篇:服务消费者(rest + ribbon)
一. ribbon简介 ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为,Feign也用到了ribbon,当你使用@ FeignClient,ribbon自动被应用. Rib ...
- 第八集:魔法阵 NTT求循环卷积
题目来源:http://www.fjutacm.com/Problem.jsp?pid=3283 题意:给两串长度为n的数组a和b,视为环,a和b可以在任意位置开始互相匹配得到这个函数的值,求这个函数 ...
- MySQL管理工具MySQL Utilities — 介绍与安装(1)
MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...
- IPsec工具之ipsec-tools
Linux从2.6内核开始自带IPsec模块,配合IPsec-Tools,可以实现Linux的IPsec功能. IPsec-Tools包含4个模块 libipsec:PF_KEY实现库 setkey: ...
- 探秘Java类加载
Java是一门面向对象的编程语言. 面向对象以抽象为基础,有封装.继承.多态三大特性. 宇宙万物,经过抽象,均可归入相应的种类.不同种类之间,有着相对井然的分别. Java中的类,便是基于现实世界中的 ...
- 解决依赖的moduleBuildConfig.DEBUG总是未false的问题
Android 开发中一般会通过 BuildConfig.DEBUG 判断是否是 Debug 模式,从而做一些在 Debug 模式才开启的特殊操作,比如打印日志.这样好处是不用在发布前去主动修改,因为 ...
- MyEclipse如何查找指定工程下所有或指定文件中特定字符串并且可进行批量替换
查找操作步骤:(1)在myEclipse里菜单选择-Search-Search(快捷键:ctrl+h);(2)在弹出对话框中选File Search选项,然后在第一个文本框中输入“要查找的字符串”(为 ...
- 实操一下<python cookbook>第三版1
这几天没写代码, 练一下代码. 找的书是<python cookbook>第三版的电子书. *这个操作符,运用得好,确实少很多代码,且清晰易懂. p = (4, 5) x, y = p p ...
- fis3-postpackager-loader
静态资源前端加载器,用来分析页面中使用的和依赖的资源(js或css), 并将这些资源做一定的优化后插入页面中.如把零散的文件合并. 注意 此插件做前端硬加载,适用于纯前端项目,不适用有后端 loade ...