Codefroces 628B New Skateboard(数位+思维)

题目链接:http://codeforces.com/contest/628/problem/B

题目大意:
给你一段数字串s(1?≤?|s|?≤?3·10^5),求该字符串有多少子串是4的倍数。
解题思路:
很容易可以想到,如果一个两位数可以被4整除,那么在它左边加任何数字都能被4整除,如24能被4整除,那么124,1324也可以。因为第3位就是百位,
只要是100的倍数肯定也是4的倍数。所以只用考虑前两位即可。

代码:

 1 #include<bits/stdc++.h>
 2 #define lc(a) (a<<1)
 3 #define rc(a) (a<<1|1)
 4 #define MID(a,b) ((a+b)>>1)
 5 #define fin(name)  freopen(name,"r",stdin)
 6 #define fout(name) freopen(name,"w",stdout)
 7 #define clr(arr,val) memset(arr,val,sizeof(arr))
 8 #define _for(i,start,end) for(int i=start;i<=end;i++)
 9 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
10 using namespace std;
11 typedef long long LL;
12 const int N=5e6+5;
13 const int INF=0x3f3f3f3f;
14 const double eps=1e-10;
15 
16 int main(){
17     FAST_IO;
18     string str;
19     cin>>str;
20     int sze=str.size();
21     LL ans=0;
22     for(int i=sze-1;i>=0;i--){
23         int num=str[i]-'0';
24         if(num%4==0)
25             ans++;
26         if(i>0){
27             int t=num+(str[i-1]-'0')*10;
28             if(t%4==0)
29                 ans+=i;            //若s[i]*10+s[i-1] 能被4整除那么数字左边无论加什么都会被4整除 
30         }
31     }
32     cout<<ans<<endl;
33     return 0;
34 }
原文地址:https://www.cnblogs.com/fu3638/p/9104316.html