题目描述
游游拿到了一个数组,她准备在其中选择两个数,使得乘积的末尾至少有x个0。游游想知道,至少有多少种不同的取数方法?
输入描述:
第一行输入两个正整数n和x,代表数组的大小以及乘积末尾0的数量。 第二行输入n个正整数ai,代表游游拿到的数组。 1≤n,x≤105 1≤ai≤109
输出描述:
输出一个整数,代表游游选择的方案数。
示例1
输入
5 2 3 5 50 2 80
输出
3
说明
5*80=400,末尾有2个0。 50*2=100,末尾有2个0。 50*80=4000,末尾有3个0。 有以上3种方案满足乘积至少有2个0。
参考了标准答案的做法,其中在移动L的while操作很值得学习
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod=1e9+7;
const int M=4e4+10;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int minn=0x3f3f3f3f;
int maxn=0xc0c0c0c0;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
ll n,m,s,e,k,h,b,l,r,x;
ll ans=0;
struct p{
int er;
int wu;
}a[100001];
bool cmp(p a,p b)
{
return a.er<b.er;
}
int t[1001];
void solve()
{
cin>>n>>x;
for(int i=1,y;i<=n;i++)
{
cin>>y;
while(y%2==0&&y)
y/=2,a[i].er++;//记录下2的个数
while(y%5==0&&y)
y/=5,a[i].wu++;//记录下5的个数
}
sort(a+1,a+1+n,cmp);//按2的个数的升序进行排序
int L=1,R=n+1;
for(int i=1;i<=n;i++)
t[a[i].wu]++;//记录5的个数的个数
while(L<R)//相当于在枚举右端点
{
R--;
t[a[R].wu]--;//排除自身的5
while(L<R&&a[L].er+a[R].er<x)//由于排过序,所以我们最后找到的区间时满足条件的区间
t[a[L].wu]--,L++;
for(int i=max((ll)0,x-a[R].wu);i<=10;i++)
ans+=t[i];
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll t=1;
// cin>>t;
while(t--)
{
solve();
}
return 0;
}