题解:B3832 [NICA #2] 回来吧我的小波

思路

经典抽屉原理。

对于长度大于 \(9\) 的子串,我们就可以认为它一定是好的,因为一定有两个数是相同的,它们可以互相整除。

对于剩下长度小于等于 \(9\) 的子串,我们对它们进行暴力枚举即可。

AC 代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
string s;
ll ans;

int val(int l,int r){
	int res=0;
	for(int i=l;i<=r;i++)res = res * 10 + (s[i]-'0');
	return res;
}

bool query(int l,int r){
	for(int l1=l;l1<r;l1++){
		for(int r1=l1;r1<r;r1++){
			for(int l2=r1+1;l2<=r;l2++){
				for(int r2=l2;r2<=r;r2++){
					if(!(val(l2,r2)%val(l1,r1)))return true;
				}
			}
		}
	}
	return false;
}

int main(){
	// freopen("text.in","r",stdin);
	// freopen("text.out","w",stdout);
	ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
	cin>>s;
	int n=s.size();
	for(int i=0;i<=n;i++){
		ans+=max(n-9-i,0);//先把长度大于9的加进去
		int l = min(i+8,n-1);
		for(int j=i+1;j<=l;j++){
			ans+=query(i,j);
		}
	}
	cout<<ans;
	return 0;
}
posted @ 2024-12-17 14:15  Zheng_iii  阅读(25)  评论(0)    收藏  举报