总结

mac2024-04-06  38

1.素数筛

void isprime(long long n)//素数筛 { memset(number,true,sizeof(number)); for(long long i=2;i<=n;i++) { if(number[i]) prime[cnt++]=i; for(long long j=0;j<cnt&&i*prime[j]<=n;j++) { number[i*prime[j]]=false; if(i*prime[j]==0) break; } } }

2.判断素数:6倍数判别法 大于等于5的质数一定和6的倍数相邻

bool prim(int a) { if(x==1) return 0; if(x==2||x==3) return 1; if(x%6!=1&&x%6!=5) return 0; int tmp=sqrt(x); for(int i=5;i<=tmp;i+=6) { if(x%i==0||x%(i+2)==0) return 0; } return 1; }

3.Polycarp likes numbers that are divisible by 3. He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33. For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by 33. Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character ‘0’). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid. What is the maximum number of numbers divisible by 33 that Polycarp can obtain?

三位数一定能被3整除

#include <iostream> #include <cstdio> #include <string> using namespace std; int main() { string a; while(cin>>a) { int t=0,sum=0,k=0,cnt=0; for(int i=0;i<a.size();i++) { int num1=a[i]-'0'; if(num1%3==0) { cnt++; } else { sum+=num1; k++; if(k>1) { if(i-t!=1) { sum=num1; k=1; } } t=i; if(sum%3==0||k==3) { cnt++; sum=k=0; } } } printf("%d\n",cnt); } return 0; }
最新回复(0)