1. 程式人生 > 其它 >Codeforces Round #779 (Div. 2) A-D1題解

Codeforces Round #779 (Div. 2) A-D1題解

A. Marin and Photoshoot

Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!

For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least 22 cosplayers, the number of males does not exceed the number of females (obviously).

Currently, the line has nn cosplayers which can be described by a binary string ss. The ii-th cosplayer is male if si=0si=0 and female if si=1si=1. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.

Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?

Input

The first line contains a single integer tt (1t1031≤t≤103) — the number of test cases.

The first line of each test case contains a positive integer nn (1n1001≤n≤100) — the number of cosplayers in the initial line.

The second line of each test case contains a binary string ss of length nn — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female.

Note that there is no limit on the sum of nn.

Output

For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful.


題目大意:在‘01‘串中新增最小數量個‘1’使其任意長度不小於2的子串中‘1’的數量大於等於2。
思路:只需保證兩個‘0’之間最少有兩個‘1’即可。值得注意的是當n等於1時不論是有一個男性或者一個女性都輸出0即可。(當時因為判斷只有一個男性輸出1而wa了3次)。

AC程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin>>t;
 8     while(t--){
 9         int n;
10         cin>>n;
11         string s;
12         cin>>s;
13         if(n==1){
14             cout<<"0"<<endl;
15             continue;
16         }
17         int z=0;
18         for(int i=0;i<n;i++){
19             if(s[i]=='0')
20                 z++;
21         }
22         if(z==0||z==1&&n>=2){
23             cout<<"0"<<endl;
24             continue;
25         }
26         int ans=0;
27         for(int i=0;i<n;i++){
28             if(s[i]=='0')
29             {
30                 for(int j=i+1;j<n;j++){
31                     if(s[j]=='0'){
32                         if(j-i==1)
33                             ans+=2;
34                         else if(j-i==2)
35                             ans+=1;
36                         i=j-1;
37                         break;
38                     }
39                 }
40             }
41         }
42         cout<<ans<<endl;
43     }
44     return 0;
45 }

B. Marin and Anti-coprime Permutation

Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!

For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least 22 cosplayers, the number of males does not exceed the number of females (obviously).

Currently, the line has nn cosplayers which can be described by a binary string ss. The ii-th cosplayer is male if si=0si=0 and female if si=1si=1. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.

Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?

Input

The first line contains a single integer tt (1t1031≤t≤103) — the number of test cases.

The first line of each test case contains a positive integer nn (1n1001≤n≤100) — the number of cosplayers in the initial line.

The second line of each test case contains a binary string ss of length nn — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female.

Note that there is no limit on the sum of nn.

Output

For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful.

題目大意:給定一個正整數n,將1~n任意排序並分別乘上其下標(下標從1開始)後得到一個新的序列,判斷有多少個這樣的序列滿足整體最大公約數不等於1。
思路:我們可以設一個x,則1~n中有n/x個數包含x的因子,在加上該乘的數(下標1~n)即有2*(n/x)個含x的因子,則滿足的序列需要滿足2*(n/x)>=n。可得x只能等於2。即序列全是奇數時不滿足,全是偶數時滿足。因此當n為奇數時答案為零,為偶數時只需讓奇數位乘上偶數,偶數位乘上奇數即可,答案為(n/2)!*(n/2)!。

AC程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t;
 4 typedef long long ll;
 5 ll n, ans;
 6 ll sss(ll k){
 7     ll iu=1;
 8     for(ll i=1;i<=k;i++){
 9         iu=(iu*(i%98244353))%998244353;
10     }
11     return iu;
12 }
13 int main()
14 {
15     std::ios::sync_with_stdio;
16     cin>>t;
17     while(t--){
18         cin>>n;
19         if(n%2!=0){
20             cout<<"0"<<endl;
21             continue;
22         }
23         ll op=n/2;
24         ll kl=sss(op);
25         ans=(kl*kl)%998244353;
26         cout<<ans<<endl;
27     }
28     return 0;
29 }

C. Shinju and the Lost Permutation

Shinju loves permutations very much! Today, she has borrowed a permutation pp from Juju to play with.

The ii-th cyclic shift of a permutation pp is a transformation on the permutation such that p=[p1,p2,,pn]p=[p1,p2,…,pn] will now become p=[pni+1,,pn,p1,p2,,pni]p=[pn−i+1,…,pn,p1,p2,…,pn−i].

Let's define the power of permutation pp as the number of distinct elements in the prefix maximums array bb of the permutation. The prefix maximums array bb is the array of length nn such that bi=max(p1,p2,,pi)bi=max(p1,p2,…,pi). For example, the power of [1,2,5,4,6,3][1,2,5,4,6,3] is 44 since b=[1,2,5,5,6,6]b=[1,2,5,5,6,6] and there are 44 distinct elements in bb.

Unfortunately, Shinju has lost the permutation pp! The only information she remembers is an array cc, where cici is the power of the (i1)(i−1)-th cyclic shift of the permutation pp. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.

Given the array cc, determine if there exists a permutation pp that is consistent with cc. You do not have to construct the permutation pp.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The input consists of multiple test cases. The first line contains a single integer tt (1t51031≤t≤5⋅103) — the number of test cases.

The first line of each test case contains an integer nn (1n1051≤n≤105).

The second line of each test case contains nn integers c1,c2,,cnc1,c2,…,cn (1cin1≤ci≤n).

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.


題目大意:這一題題目好繞,英語不好屬實難受。大概就是由一個全排列得到一個數組,陣列第i個數是全排列中1~i中最大的數。而陣列中不相同的數的個數稱為全排列的冪。那麼可以將全排列整體右移一個單位(相當於最後一個提到第一)後等到另一個冪。將這些冪依次存入C陣列,問是否存在一個全排列令移動i次的值等於Ci的值。
思路:判斷兩個特有情況。
1.出現冪等於1的情況有且只有一種。
2.因為每次移動相當於從後提出一位到最前方,所以如果右移後冪增加,那麼增加的數量肯定不會超過1。
只有同時滿足這兩種情況的全排列才是滿足題意的全排列。

AC程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t,n,c[100001];
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin.tie(0);
 8     cin>>t;
 9     while(t--){
10         cin>>n;
11         for(int i=1;i<=n;i++){
12             cin>>c[i];
13         }
14         c[n+1]=c[1];
15         int flag=1,sum=0;
16         for(int i=1;i<=n;i++){
17             if(c[i+1]-1>c[i])
18                 flag=0;
19             if(c[i]==1)
20                 sum++;
21         }
22         if(flag&&sum==1)
23             cout<<"YES"<<endl;
24         else
25             cout<<"NO"<<endl;
26     }
27     return 0;
28 }

D1. 388535 (Easy Version)

This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.

Marin and Gojou are playing hide-and-seek with an array.

Gojou initially performs the following steps:

  • First, Gojou chooses 22 integers ll and rr such that lrl≤r.
  • Then, Gojou makes an array aa of length rl+1r−l+1 which is a permutation of the array [l,l+1,,r][l,l+1,…,r].
  • Finally, Gojou chooses a secret integer xx and sets aiai to aixai⊕x for all ii (where ⊕ denotes the bitwise XOR operation).

Marin is then given the values of l,rl,r and the final array aa. She needs to find the secret integer xx to win. Can you help her?

Note that there may be multiple possible xx that Gojou could have chosen. Marin can find any possible xx that could have resulted in the final value of aa.

Input

The first line contains a single integer tt (1t1051≤t≤105) — the number of test cases.

In the first line of each test case contains two integers ll and rr (0=lr<2170=l≤r<217).

The second line contains rl+1r−l+1 integers of a1,a2,,arl+1a1,a2,…,ar−l+1 (0ai<2170≤ai<217). It is guaranteed that aa can be generated using the steps performed by Gojou.

It is guaranteed that the sum of rl+1r−l+1 over all test cases does not exceed 217217.

Output

For each test case print an integer xx. If there are multiple answers, print any.

D
題目大意:給出一個0到r的序列(題目給的是l,但l恆等於0)另外給你一個序列a,找出一個整數與序列a整體異或為0到r的一個全排列。
思路:觀察0~7的二進位制可發現每一位上0的數量肯定大於等於1,所以對於給定的序列,對於每一位0>=1的位置取0,反之取1便得到答案的二進位制形式。

AC程式碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int t,op[20][2];
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin.tie(0),cout.tie(0);
 8     cin>>t;
 9     while(t--)
10     {
11         int l,r;
12         cin>>l>>r;
13         memset(op,0,sizeof(op));
14         for(int i=0;i<=r;i++)
15         {
16             int a;
17             cin>>a;
18             for(int j=0;j<=17;j++)
19                 op[j][a>>j&1]++;
20         }
21         int ans=0;
22         for(int i=0;i<=17;i++)
23         {
24             if(op[i][0]<op[i][1])
25                 ans+=1<<i;
26         }
27         cout<<ans<<endl;
28     }
29     return 0;
30 }