Problem Description
MG is a rich boy. He has n apples, each has a value of V(0<=V<=9).
A valid number does not contain a leading zero, and these apples have just made a valid N digit number.
MG has the right to take away K apples in the sequence, he wonders if there exists a solution: After exactly taking away K apples, the valid N−K digit number of remaining apples mod 3 is zero.
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
A valid number does not contain a leading zero, and these apples have just made a valid N digit number.
MG has the right to take away K apples in the sequence, he wonders if there exists a solution: After exactly taking away K apples, the valid N−K digit number of remaining apples mod 3 is zero.
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
Input
The first line is an integer T which indicates the case number.(1<=T<=60)
And as for each case, there are 2 integer N(1<=N<=100000),K(0<=K<N) in the first line which indicate apple-number, and the number of apple you should take away.
MG also promises the sum of N will not exceed 1000000。
Then there are N integers X in the next line, the i-th integer means the i-th gold’s value(0<=X<=9).
And as for each case, there are 2 integer N(1<=N<=100000),K(0<=K<N) in the first line which indicate apple-number, and the number of apple you should take away.
MG also promises the sum of N will not exceed 1000000。
Then there are N integers X in the next line, the i-th integer means the i-th gold’s value(0<=X<=9).
Output
As for each case, you need to output a single line.
If the solution exists, print”yes”,else print “no”.(Excluding quotation marks)
If the solution exists, print”yes”,else print “no”.(Excluding quotation marks)
Sample Input
2
5 2
11230
4 2
1000
Sample Output
yes
no
题意:
思路:
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int a[];
char s[]; void cal(int &a3, int &E1,int &E2,int N)
{
a3=; E1=; E2=;
for(int i=;i<=N;i++)
{
if(a[i]==) break;
if(a[i]==) a3++;
}
for(int i=;i<=N;i++)
{
if(a[i]==) break;
if(a[i]==) E1=;
if(a[i]==) E2=;
}
return ;
} int main()
{
int T;
cin>>T;
while(T--)
{
int N,K;
int s1=,s2=,s3=;
scanf("%d%d",&N,&K);
scanf("%s",s+);
for(int i=;i<=N;i++)
{
a[i]=s[i]-'';
if(a[i]%==) a[i]=,s1++;
else if(a[i]%==) a[i]=,s2++;
else s3++,a[i]=(a[i])?:;
}
int ans=(s1+s2*)%;
int a3,E1,E2,f=;
cal(a3,E1,E2,N);
for(int C=;C<=s2&&C<=K;C++) ///C->2; B->1; A->0;
{
int B=((ans-C*)%+)%;
for(;B<=s1&&C+B<=K;B=B+)
{
int A=K-C-B;
if(A<=s3)
{
if(A>a3) f=;
else if(B<s1&&E1) f=;
else if(C<s2&&E2) f=;
if(f) break;
}
}
if(f) break;
}
if((N==K+)&&s3) f=;
if(f) puts("yes");
else puts("no");
}
return ;
}