题解:B4185 [中山市赛 2024] 倍数子串
B4185 [中山市赛 2024] 倍数子串
思路:
看样例:04320
先看04:以04或4为结尾的连续子串有
04,4
再看43:以43或3为结尾的连续子串有
43?
实际上什么也没有
接着看32:以32或2为结尾的连续子串有
32,432,0432
最后再看20:以20或0为结尾的连续子串有
0,20,320,4320,04320
好的就是十个直接上代码
别忘了开头的0
别忘了开头的0
别忘了开头的0
重要的事说三遍
大家应该有个思路了
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
string s;
int n,ans=0;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s;
n=s.size();
int s0=s[0]-'0';
if(s0%4==0||s0%5==0) ans++;
for(int i=0;i<=n-2;i++)
{
int t=s[i]-'0',o=s[i+1]-'0',sum;
sum=t*10+o;
if(o%4==0||o%5==0) ans++;
if(sum%4==0||sum%5==0) ans+=i+1;
}
cout<<ans;
return 0;
}
OK🆗就这样了
第一次写题解 还不太会 不喜勿喷