HDU6020 MG loves apple

MG loves apple

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 442    Accepted Submission(s): 76


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  NK  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 ).
 

Output
As for each case, you need to output a single line.

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
 

Source
 

官方题解:

这题更多算是想法题,有脑洞的同学可以各种乱搞。将问题转化成:求去掉KK位数字后,不含前导零,且数字和是否能被三整除。按照预测,这里应该会汇聚本场最多的hack点。

我们设S0S0S1S1S2S2分别为原串上mod 3=0mod3=01122数字的个数。 我们假定删除取模后为001122的数字各AABBCC个,则显然有0<=A<=S0,0<=B<=S1,0<=C<=S20<=A<=S0,0<=B<=S1,0<=C<=S2K=A+B+CK=A+B+CSumSum mod 3=(A*0+B*1+C*2)mod 3=(S0*0+S1*1+S2*2)mod 3=biasmod3=(A0+B1+C2)mod3=(S00+S11+S22)mod3=bias。 枚举CC的值,我们可得B mod 3=(bias-C*2)mod 3,A=K-B-CBmod3=(biasC2)mod3,A=KBC。如果有若干组A,BA,B不逾界,可知这些(A,B,C)(A,B,C)是在模意义下合法的解,但不一定满足没有前导零。

所以,对于【大于00的数】我们贪心地从后往前删除,对于00我们贪心地从前往后删除。

需要统计出:a3a3=第一个【mod 3=0mod3=0且非00的数】前00的个数(如果mod 3=0mod3=0且非00的数不存在,那么a3a3就取所有零的个数),E1E1=【第一个00前是否存在mod 3=1mod3=1的数】,E2E2=【第一个00前是否存在mod 3=2mod3=2的数】。

则以下情况满足任一种都能保证无前导零:A>=a3A>=a3B<B<S1S1E1E1C<C<S2S2E2E2


还需要加一种情况 满足k==n-1 也是yes


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<string.h>
#include<math.h>
#include<list>

using namespace std;

#define ll long long
#define pii pair<int,int>
const int inf = 1e9 + 7;

const int N = 100000 + 5;
char ch[N];
int x[N];

bool slove(int n,int k){
    for(int i=0;i<n;++i){
        x[i]=ch[i]-'0';
        x[i]%=3;
    }
    int s[3]={0,0,0};
    int sum=0;
    for(int i=0;i<n;++i){
        s[x[i]]+=1;
        sum+=x[i];
    }
    int a3=0;//>0&&mod3==0的第一数 前面的0的个数
    bool e1,e2=e1=0;
    for(int i=0;i<n;++i){
        if(ch[i]!='0'&&x[i]==0){
            break;
        }
        a3+=(x[i]==0);
        if(x[i]==1){
            e1=1;
        }
        if(x[i]==2){
            e2=1;
        }
    }
    for(int c=0;c<=s[2];++c){
        int minb=((sum-2*c)%3 + 3)%3;
        for(int b=minb;b<=s[1];b+=3){
            int a=k-b-c;
            if(a>=0&&a<=s[0]){
                if((a>=a3)||(e1&&b<s[1])||(e2&&c<s[2])){
                    return 1;
                }
                if(k==n-1){
                    return 1;
                }
            }

        }
    }
    return 0;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        int n,k;
        scanf("%d%d%s",&n,&k,ch);
        printf("%s\n",slove(n,k)?"yes":"no");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值