Kevin喜欢零——分解加二分

题目来自《牛客小白月赛73》:牛客小白月赛73_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)

输入

2
5 3
125 1 8 1 1
1 0

输出

3

思路:

   

 AC代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 2e5+5;
int t,n,k;
int a[N],c2[N],c5[N],p2[N],p5[N];
signed main()
{
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(int i=1;i<=n;i++) c2[i]=c5[i]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			while(a[i]%2==0){
				a[i]/=2,c2[i]++; 
			}
			while(a[i]%5==0){
				a[i]/=5,c5[i]++; 
			}
			p2[i]=p2[i-1]+c2[i];
			p5[i]=p5[i-1]+c5[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			int v2=k+p2[i-1];
			int v5=k+p5[i-1];
			int l2=lower_bound(p2+1,p2+1+n,v2)-p2,r2=upper_bound(p2+1,p2+1+n,v2)-p2-1;
			int l5=lower_bound(p5+1,p5+1+n,v5)-p5,r5=upper_bound(p5+1,p5+1+n,v5)-p5-1;
			int l=max(l2,l5),r=max(r2,r5);
            if(l==n+1) break;//当二分找不到时,就证明没有答案退出循环 
			l=max(l,i);//该句是l可能会小于当前遍历的i,但是在i较小时已经算过了,所以这时取i 
			ans+=(r-l+1);
		}
		cout<<ans<<endl;
	}
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/weixin_61725823/article/details/131354308