zoukankan      html  css  js  c++  java
  • 2020牛客寒假算法基础集训营4 子段乘积

    https://ac.nowcoder.com/acm/contest/3005/C

    题意

      给出一个长度为 n 的数列 a1,a2,…,an,求其长度为 k 的连续子段的乘积对 998244353 取模余数的最大值。

    题解

      尺取法。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int mod = 998244353;
    long long arr[200005];
    long long inv(long long a,long long b)
    {
        long long ans=1;
        while(b)
        {
            if(b&1) ans=ans*a%mod;
            b/=2;
            a=(a*a)%mod;
        }
        return ans;
    }
    int main()
    {
        long long ans=-1,temp=1,cnt=0,i,n,k;
        scanf("%lld%lld",&n,&k);
        for(i=0;i<n;i++) scanf("%lld",&arr[i]);
        for(i=0;i<k;i++)
        {
            if(arr[i]==0) cnt++;
            else temp=temp*arr[i]%mod;
        }
        for(i=0;i<n-k;i++)
        {
            if(!cnt) ans=max(ans,temp);
            
            if(arr[i]==0) cnt--;
            else temp=temp*inv(arr[i],mod-2)%mod;
    
            if(arr[i+k]==0) cnt++;
            else temp=temp*arr[i+k]%mod;
        }
        printf("%lld",ans);
        system("pause");
        return 0;
    }
  • 相关阅读:
    JS 集合
    JS 字典
    JS 链表
    JS 队列
    JS 栈
    JS 列表
    JS 数组
    IOS 提示无法下载程式问题
    ubuntu 下安装Go开发环境
    菜鸟看Redis(一)
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/12308463.html
Copyright © 2011-2022 走看看