大数删除k位是否能整除3,MG loves apple(HDU)
2017-04-11 08:16
465 查看
传送门
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 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).
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
BestCoder Round #93
题意:求去掉KK位数字后,不含前导零,且数字和是否能被三整除。
我们设S0S0、S1S1、S2S2分别为原串上mod
3=0mod3=0、11、22数字的个数。
我们假定删除取模后为00、11、22的数字各Aa、Bb、Cc个,则显然有0<=A<=S0,0<=B<=S1,0<=C<=S20<=A<=S0,0<=B<=S1,0<=C<=S2且K=A+B+CK=A+B+C且Sum
mod3=(A∗0+B∗1+C∗2)mod3=(S0∗0+S1∗1+S2∗2)mod3=bias。
枚举CC的值,我们可得
mod
3=(A*0+B*1+C*2)mod 3=(S0*0+S1*1+S2*2)mod 3=biasB
mod 3=(bias-C*2)mod 3,A=K-B-CBmod3=(bias−C∗2)mod3,A=K−B−C。如果有若干组A,BA,B不逾界,可知这些(A,B,C)(A,B,C)是在模意义下合法的解,但不一定满足没有前导零。
所以,对于【大于00的数】我们贪心地从后往前删除,对于00我们贪心地从前往后删除。
需要统计出:a3E3=第一个【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>=E3。B<B<S1S1且E1E1。C<C<S2S2且E2E2。
还需要加一种情况 满足k==n-1 也是yes:
AC代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<map> #include<string> #define LL long long int #define inf 0x3f3f3f3f #define N 100010 using namespace std; char aa ; int main() { int t; scanf("%d",&t); while(t--) { int n,k,s0=0,s1=0,s2=0,E1=0,E2=0,E3=0,flagE3=1,flagE1=1,flagE2=1; scanf("%d%d",&n,&k); scanf("%s",aa); int sum=0; for(int i=0;i<n;i++) { int t=aa[i]-'0'; sum=(sum+t)%3; if(flagE3&&t%3==0) { flagE1=0; flagE2=0; if(t==0) E3++; else flagE3=0; } if(flagE2&&t%3==2) E2=1; if(flagE1&&t%3==1) E1=1; if(t%3==0) s0++; if(t%3==1) s1++; if(t%3==2) s2++; } int ans=0; for(int c=0;c<=s2;c++) { int minb=((sum-c*2)%3+3)%3;//(sum-c*2)%3可能为-1或-2或0,1,2加上3之后再取余3,余数一定为非负。 for(int b=minb;b<=s1;b+=3) { int a=k-b-c; if(a>=0&&a<=s0) { if((a>=E3)||(b<s1&&E1)||(c<s2&&E2)) { ans=1; break; } if(k==n-1) { ans=1; break; } } } } if(ans) printf("yes\n"); else printf("no\n"); } }再推荐一道好题:https://oj.ejq.me/problem/24 (大数整除6的最大位数)
我的博客http://blog.csdn.net/xiangaccepted/article/details/69951928有这道题的解法(用的是dp);
相关文章推荐
- HDU 6020 MG loves apple
- HDU - 6020 MG loves apple (贪心+思维+模拟)
- hdu 6020 MG loves apple 恶心模拟
- 【HDU 6020】 MG loves apple (乱搞?)
- HDU 6020 MG loves apple
- HDU-6019 MG loves gold
- HDU 6019 MG loves gold
- HDU 2054 判断大数是否相等
- (hdu step 9.1.1)A == B ?(在这个数字有可能是大数并且存在无效0的情况下,判断这两个数字是否相等)
- hdu 6206 Apple(Java大数)
- HDU 6019 MG loves gold
- hdu 4873 ZCC Loves Intersection(大数+概率)
- hdu(求n!是否能被m整除)
- hdu 6021 MG loves string 子集 容斥 循环节
- HDU 6206 Apple 2017ICPC青岛网赛 A (高精度+大数模版)
- HDU 6021 MG loves string(容斥原理)——BestCoder Round #93 1003
- nyoj1086是否被整除(大数)
- HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)
- HDU 5832:A water problem (大数整除)
- HDU - 6019 ——MG loves gold