题意:
给你一串数,问你如题。
思路:
我不是这样的作法,从后往前,先取00,再算%3==0的个数,往前推的时候有递推关系:
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream> 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<long long, vector<long long>, greater<long long> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define fo(a,b,c) for(register long long a=b;a<=c;++a) 24 #define fr(a,b,c) for(register long long a=b;a>=c;--a) 25 #define mem(a,b) memset(a,b,sizeof(a)) 26 #define pr printf 27 #define sc scanf 28 void swapp(long long &a,long long &b); 29 double fabss(double a); 30 long long maxx(long long a,long long b); 31 long long minn(long long a,long long b); 32 long long Del_bit_1(long long n); 33 long long lowbit(long long n); 34 long long abss(long long a); 35 //const long long INF=(1LL<<60); 36 const double E=2.718281828; 37 const double PI=acos(-1.0); 38 const long long inf=(1<<29); 39 const double ESP=1e-9; 40 const long long mod=(long long)1e9+7; 41 const long long N=(long long)1e6+6; 42 43 char s[N]; 44 long long ans=0; 45 46 int main() 47 { 48 sc("%s",s+1); 49 s[0]='&'; 50 long long l=strlen(s)-1; 51 long long cont=0; 52 long long cnt[4]={0,0,0,0},temp=0; 53 fr(i,l,1) 54 { 55 temp+=s[i]-'0'; 56 temp%=3; 57 cnt[temp]++; 58 if(s[i]=='0') 59 cont++; 60 } 61 fr(i,l,1) 62 { 63 if(s[i]==s[i+1]&&s[i]=='0') 64 ans+=cnt[0]; 65 temp=s[i]-'0'; 66 temp%=3; 67 if(temp==0) 68 cnt[0]--; 69 else if(temp==1) 70 { 71 long long a=cnt[2],b=cnt[1],c=cnt[0]; 72 cnt[0]=b-1;cnt[1]=a,cnt[2]=c; 73 } 74 else 75 { 76 long long a=cnt[2],b=cnt[1],c=cnt[0]; 77 cnt[0]=a-1,cnt[1]=c,cnt[2]=b; 78 } 79 } 80 81 pr("%lld ",ans+cont); 82 return 0; 83 } 84 85 /**************************************************************************************/ 86 87 long long maxx(long long a,long long b) 88 { 89 return a>b?a:b; 90 } 91 92 void swapp(long long &a,long long &b) 93 { 94 a^=b^=a^=b; 95 } 96 97 long long lowbit(long long n) 98 { 99 return n&(-n); 100 } 101 102 long long Del_bit_1(long long n) 103 { 104 return n&(n-1); 105 } 106 107 long long abss(long long a) 108 { 109 return a>0?a:-a; 110 } 111 112 double fabss(double a) 113 { 114 return a>0?a:-a; 115 } 116 117 long long minn(long long a,long long b) 118 { 119 return a<b?a:b; 120 }