Codeforces Round #616(Div.2) Even But Not Even
Posted bethebestone
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #616(Div.2) Even But Not Even相关的知识,希望对你有一定的参考价值。
Let‘s define a number ebne (even but not even) if and only if its sum of digits is divisible by 22 but the number itself is not divisible by 22. For example, 1313, 12271227, 185217185217 are ebne numbers, while 1212, 22, 177013177013, 265918265918 are not. If you‘re still unsure what ebne numbers are, you can look at the sample notes for more clarification.
You are given a non-negative integer ss, consisting of nn digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn‘t contain leading zeros. You can delete any number of digits between 00 (do not delete any digits at all) and n−1n−1.
For example, if you are given s=s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 →→ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 7070 and is divisible by 22, but number itself is not divisible by 22: it means that the resulting number is ebne.
Find any resulting number that is ebne. If it‘s impossible to create an ebne number from the given number report about it.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤30001≤n≤3000) — the number of digits in the original number.
The second line of each test case contains a non-negative integer number ss, consisting of nn digits.
It is guaranteed that ss does not contain leading zeros and the sum of nn over all test cases does not exceed 30003000.
Output
For each test case given in the input print the answer in the following format:
- If it is impossible to create an ebne number, print "-1" (without quotes);
- Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It‘s not necessary to minimize or maximize the number of deleted digits.
Example
4 4 1227 1 0 6 177013 24 222373204424185217171912
1227 -1 17703 2237344218521717191
Note
In the first test case of the example, 12271227 is already an ebne number (as 1+2+2+7=121+2+2+7=12, 1212 is divisible by 22, while in the same time, 12271227 is not divisible by 22) so we don‘t need to delete any digits. Answers such as 127127 and 1717 will also be accepted.
In the second test case of the example, it is clearly impossible to create an ebne number from the given number.
In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 11 digit such as 1770317703, 7701377013 or 1701317013. Answers such as 17011701 or 770770 will not be accepted as they are not ebne numbers. Answer 013013 will not be accepted as it contains leading zeroes.
Explanation:
- 1+7+7+0+3=181+7+7+0+3=18. As 1818 is divisible by 22 while 1770317703 is not divisible by 22, we can see that 1770317703 is an ebne number. Same with 7701377013 and 1701317013;
- 1+7+0+1=91+7+0+1=9. Because 99 is not divisible by 22, 17011701 is not an ebne number;
- 7+7+0=147+7+0=14. This time, 1414 is divisible by 22 but 770770 is also divisible by 22, therefore, 770770 is not an ebne number.
In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 →→ 22237320442418521717191 (delete the last digit).
解题思路:
判断奇数的个数
k<2 说明无论如何都不能达到要求
k>2时
k%2==0 说明只需要将末尾的偶数删去即可
k%2==1 说明需要删掉末尾的偶数,同时再删去一个奇数即可(ps:从末尾删,以避免类似013的情况出现)
代码如下:
1 #include<bits/stdc++.h> 2 #define mem(a) memset(a,0,sizeof(a)) 3 #define forn(i,n) for(int i=0;i<n;++i) 4 #define for1(i,n) for(int i=1;i<=n;++i) 5 #define IO std::ios::sync_with_stdio(false); std::cin.tie(0) 6 using namespace std; 7 typedef long long ll; 8 const int maxn=1e6+5; 9 const int inf=0x3f3f3f3f; 10 11 int n,m,s,t,k; 12 int num[maxn]; 13 char c; 14 15 int main() 16 { 17 IO; 18 cin>>t; 19 while(t--) 20 { 21 k=0; 22 cin>>n; 23 for1(i,n) 24 { 25 cin>>c; 26 num[i]=c-‘0‘; 27 if(num[i]%2==1) 28 k++; 29 } 30 //cout<<"k="<<k<<endl; 31 if(k<2) 32 { 33 cout<<"-1"<<endl; 34 } 35 else 36 { 37 if(k%2==0) 38 { 39 for(int i=n; i>=1; --i) 40 { 41 if(num[i]%2==0) 42 n--; 43 else 44 break; 45 } 46 } 47 else 48 { 49 for(int i=n; i>=1; --i) 50 { 51 if(num[i]%2==0) 52 n--; 53 else 54 break; 55 } 56 n--; 57 for(int i=n; i>=1; --i) 58 { 59 if(num[i]%2==0) 60 n--; 61 else 62 break; 63 } 64 } 65 for1(i,n) 66 if(num[i]!=-1) 67 cout<<num[i]; 68 cout<<endl; 69 } 70 } 71 return 0; 72 }
完毕!
以上是关于Codeforces Round #616(Div.2) Even But Not Even的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #616 (Div. 2)
Codeforces Round #616 (Div. 2)
Codeforces Round #616 (Div. 1)
Codeforces Round #616(Div.2) Even But Not Even