CodeForces 628 B.New Skateboard (簡單數學)

題意:

給一個數字字符串,問有多少個子串是4的倍數

思路:

以前寫過一題:CodeForces 550C
這題的方法:
因爲1000是8的倍數,則只需要看最後三位是否8的倍數,暴力判長度一二三位即可

現在這題:
因爲100是4的倍數,則只需要後兩位是4的倍數即可,暴力判長度一二位即可
然後這兩位的左邊接任意數都是4的倍數

ps:
其他類似題應該也是這個套路

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=3e5+5;
char s[maxm];
signed main(){
    scanf("%s",s+1);
    int n=strlen(s+1);
    for(int i=1;i<=n;i++){
        s[i]-='0';
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        if(s[i]%4==0)ans++;//單個數
        int temp=s[i-1]*10+s[i];
        if(temp%4==0){
            ans++;//這兩位本身
            ans+=i-2;//這兩位左邊隨便加
        }
    }
    cout<<ans<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章