日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Xtreme9.0 - Light Gremlins 容斥

發布時間:2024/1/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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(); }

轉載于:https://www.cnblogs.com/qscqesze/p/5954466.html

總結

以上是生活随笔為你收集整理的Xtreme9.0 - Light Gremlins 容斥的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。