寒意


私信TA

用户名:uq_12535881956

访问量:1472

签 名:

等  级
排  名 11713
经  验 955
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:首先对输入进行转化,将多个一转化为一个负数存放。

例:1 1 1 1 3 1 1 1 1 1 5 6

输入的时候是e数组,实际上用的ne数组里的值就是

-4 3 -5 5 6

因为本题要是有个大的区间符合和与积相等,那么必然有很多很多个一

例:3 60000 119997个1

那么这样一个119999长度的区间也是符合的

或者说无论前面乘积多少,只要后面加上乘积减去和的个数个1,就能组成一个区间。


区间为1的都符合,故不予考虑。

接下来是选开头,每个数都能成为开头,所以如果ne[i]是负数,就要有相应个数个开头。

t为乘积,p为和。

有了开头,接下来就是重点的遍历了。

遍历开头后的每一个ne元素

如果ne[k]小于0,那么就说明后面有多少个1

此时有两种情况

t<=p

那么因为后面都是1,所以t的值不变,p的值变大,加多少个一都是白搭(t!=p)

t>p

如果t-p小于ne所代表的1的个数,那么一定能有一个时候在不断加1后使t==p,之后加1就像上一个情况一样,之后都是p>t

如果ne[k]>0

那么t*=ne[k],p+=ne[k],即更新t和p的值,如果更新后相等,那么ans++

最后加个特判,如果t的值过大,大到后面全是1也加不过来的时候直接用break退出,这里我选的是3*n,反正过了,否则保险一点10*n甚至更大应该都是能过的。


最后输出ans+n即可



注意事项:

参考代码:

#include<iostream>

#include<cstring>

using namespace std;

const int N=200010;

typedef long long LL;

int e[N],ne[N],a,b,c,d,n,m,idx,ans;

int main()

{

    cin>>n;

    idx=1;

    for(int i=1;i<=n;i++)

    {

        scanf("%d",&e[i]);

        if(idx==0&&e[i]==1)

        {

            ne[a]--;

        }

        else if(idx==0&&e[i]!=1)

        {

            ne[++a]=e[i];

            idx=1;

        }

        else if(idx==1&&e[i]==1)

        {

            ne[++a]--;

            idx=0;

        }

        else ne[++a]=e[i];

    }

    LL t,p,q;

    for(int i=1;i<=a;i++)

    {

        if(ne[i]<0)

        {

            for(int j=-ne[i];j>=1;j--)

            {

                t=1,p=j;

                for(int k=i+1;k<=a;k++)

                {

                    if(ne[k]<0)

                    {

                        if(t-p>0&&t-p<=-ne[k])

                        {

                            ans++;

                        }

                        p+=-ne[k];

                    }

                    else 

                    {

                        t*=ne[k];p+=ne[k];

                        if(t==p)

                        ans++;

                    }

                    if(t>3*n)

                    {

                        break;

                    }

                }

            }

        }

        else 

        {

            t=p=ne[i];

            for(int k=i+1;k<=a;k++)

            {

                if(ne[k]<0)

                {

                    if(t-p>0&&t-p<=-ne[k])

                    {

                        ans++;

                    }

                    p+=-ne[k];

                }

                else 

                {

                    t*=ne[k];p+=ne[k];

                    if(t==p)

                    ans++;

                }

                if(t>3*n)

                {

                    break;

                }

            }

        }

    }

    cout<<ans+n<<endl;

    return 0;

}


 

0.0分

8 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

muls = muls[:j + 1]   
        while muls and muls[0][0] > total:
            muls = muls[1:]
        for mul in muls:
            if curSum - mul[0] in mp and mul[1] <= mp[curSum - mul[0]] < mul[2]:
                res += 1
        mp[curSum] = i + 1
    print(res)
2024-03-06 20:39:14
python简介代码
def solve():
    n = II()
    a = LII()
    total = sum(a)
    mp = defaultdict(int)
    mp[0] = 0
    muls = []
    res = curSum = 0
    for i, x in enumerate(a):
        curSum += x
        muls = [[mul[0] * x, mul[1], mul[2]] for mul in muls]
        muls.append([x, i, i + 1])
        j = 0
        for mul in muls[1:]:
            if muls[j][0] == mul[0]:
                muls[j][2] = mul[2]
            else:
                j += 1
                muls[j] = mul
        muls = muls[:j + 1]   
        while muls and muls[0][0] > total:
            muls = muls[1:]
        for mul in
2024-03-06 20:38:28
ORZ
2023-05-31 20:57:39
python

n = int(input())
a = input().split()
for i in range(len(a)):
    a[i] = int(a[i])
answer = 0
for i in range(0, n):
    multiplication = a[i]
    add = a[i]
    for j in range(i, n):
        if i != j:
            multiplication = multiplication*a[j]
            add = add+a[j]
        if multiplication == add:
            answer += 1
print(answer)
2022-04-07 16:26:02
  • «
  • 1
  • »